Javaクラスのコンストラクション関数
9071 ワード
package com.fish.object;
/*
, :id\ name \ age 。
: 。
: , 。
*/
class Employee{
int id; //
String name; //
int age; //
//
public Employee(int a, String b , int c){
id =a;
name = b;
age = c;
}
public void work(){
System.out.println(name+" , !!");
}
}
class Demo3{
public static void main(String[] args){
//
Employee e = new Employee(110," ",20);
System.out.println(" :"+ e.id+" :"+e.name +" :"+ e.age);
}
}
package com.fish.object;
/*
:
: 。new , ,
: 。
:
{
}
: 。 ,
:
1. 。
2. . 。 : , 。
3. static
*/
class Baby {
int id; // ***
String name; //
// ...
{
System.out.println(" ......");
}
//
public Baby(int i, String n) {
id = i;
name = n;
}
//
public Baby() {
}
public void cry() {
System.out.println(name + " ...");
}
}
class Demo4 {
public static void main(String[] args) {
{ //
Baby b1 = new Baby(110, " "); //
System.out.println(" :" + b1.id + " :" + b1.name);
}
/*
* System.out.println(" :"+ b1.id + " :"+b1.name);
*
* new Baby(112," "); new Baby();
*/
/*
* new Baby(110," "); new Baby(112," "); new Baby();
*/
}
}
package com.fish.object;
/*
:
1. java java , 。
2. 。
3. java , , , 。
4. 。
*/
class Demo5 {
//
public Demo5() { //
i = 300000000;
}
// //
{
i = 200000000;
}
int i = 100000000; //
public static void main(String[] args) {
Demo5 d = new Demo5();
System.out.println("i = " + d.i);
}
}
package com.fish.object;
/*
: java 。
: , (java 。)。
this :
this 。
this :
1. , , this 。
2. 。
this :
1. this ,this 。
2. this , 。
3. this new ,
this :
1. , (java “ ” 。)
2. , , java this 。
*/
class Animal {
String name; //
String color;
public Animal(String n, String c) {
name = n;
color = c;
}
// this
public void eat() {
System.out.println("this:"+ this);
String name = " "; //
System.out.println(this.name + " ..."); // : name name.
}
}
class Demo6 {
public static void main(String[] args) {
Animal dog = new Animal(" ", " "); // name 。
Animal cat = new Animal(" ", " ");
cat.eat();
}
}
package com.fish.object;
/*
this :
1. this ,this 。
2. this , 。
*/
class Student {
int id; // ***
String name; //
// : , 。
public Student(int id, String name) { // 。
this(name); //
//this.name=name; //
// this(); // 。
this.id = id; // this.id = id id id
System.out.println(" ...");
}
public Student() {
System.out.println(" ...");
}
public Student(String name) {
this.name = name;
System.out.println(" ...");
}
}
class Demo7 {
public static void main(String[] args) {
Student s = new Student(110, " ");
System.out.println(" :" + s.id + " :" + s.name);
/*
*
* Student s2 = new Student(" "); System.out.println(" :" + s2.name);
*/
}
}
package com.fish.object;
/*
: java , id、name 、 age , 。
: , this 。
*/
class Person{
int id; //
String name; //
int age; //
//
public Person(int id,String name ,int age){
this.id = id;
this.name = name;
this.age = age;
}
//
public void compareAge(Person p2){
if(this.age>p2.age){
System.out.println(this.name+" !");
}else if(this.age<p2.age){
System.out.println(p2.name+" !");
}else{
System.out.println(" ");
}
}
}
class Demo8{
public static void main(String[] args)
{
Person p1 = new Person(110," ",17);
Person p2 = new Person(119," ",9);
p1.compareAge(p2);
//p1.compareAge(p1,p2); //
}
}
package com.fish.object;
/*
static( )
: 。 ....
: , n n ,
。
: “ ” , Student 。
2: ?
: static 。
, 。。
*/
class Students{
String name;
// static country, country 。
static String country = " ";//
//
public Students(String name){
this.name = name;
}
}
class Demo9 {
public static void main(String[] args)
{
Students s1 = new Students(" ");
Students s2 = new Students(" ");
s1.country = " "; // , ,
System.out.println(" :"+s1.name+" :"+ s1.country); //
System.out.println(" :"+s2.name+" :"+ s2.country); //
}
}
package com.fish.object;
/*
static( \ )
1. static : , static 。
:
1: 。
: . 。
: 。
: . ;
:
1. , 。
2. static ,
static 。
static : , static 。
2. static :
*/
class Student2{
staticString name; //
staticString country = " "; //
public Student2(String name){
this.name = name;
}
}
class Demo10 {
public static void main(String[] args){
Student2 s1 = new Student2(" ");
Student2 s2 = new Student2(" ");
//System.out.println(" :"+ Student.country);
System.out.println(" :"+ s1.name);
System.out.println(" :"+ s2.name);
}
}
package com.fish.object;
/*
: , 。
*/
class Emp{
// 。
staticint count = 0;//
String name;
//
{
count++;
}
public Emp(String name){
this.name = name;
}
public Emp(){ //
}
public void showCount(){
System.out.println(" "+ count+" ");
}
}
class Demo11{
public static void main(String[] args){
Emp e1 = new Emp();
Emp e2 = new Emp();
Emp e3 =new Emp();
e3.showCount();
}
}
/*
:
: 。
:
( ){
}
:
1. 。
2. 。
3. , ,jvm
。
4. , java 。
5. , java 。
6. 。
:
: 。
:
{
;
}
。
this :
this 。
this :
1. , , this 。
2. this ,
this :
1. this , 。
2. this , 。
3. , , , java this 。
static( 、 )
static :static , .
:
: 。
.
: 。
.
:
1. , 。
2. static , static 。
*/
class Demo1{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
本文は“小さい魚のブログ”のブログから出て、転載をお断りします!