*/ public class dfssc { public static void main (String []args) { /*Dog dog1=new Dog(2,"小黄"); Person p1=new Person(dog1,23,"猪坚强"); Person p2=new Person(dog1,24,"狗坚弱"); p1.showInfo(); p1.dog.showInfo();*/ //int total=0; Child ch1=new Child(2,"妞妞"); ch1.joinGame(); //total++; Child ch2=new Child(4,"小小"); ch2.joinGame(); Child ch3=new Child(5,"大大"); ch3.joinGame(); //total++; System.out.println("共有="+ch3.total); } } //定义一个人类 class Person { //成员变量 int age; String name; Dog dog;//应用类型 public Person (Dog dog,int age,String name) { //可读性比较差 //this属于一个对象 this.age=age; this.name=name; this.dog=dog; } // public void showInfo() { System.out.println("人名是:"+this.name); } } class Dog { int age; String name; public Dog(int age,String name) { this.age=age; this.name=name; } //显示狗名 public void showInfo() { System.out.println("狗名叫"+this.name); } } //定义一个小孩类 class Child { int age; String name; //total是静态变量,因此他可以被任何对象访问 static int total=0; public Child(int age,String name) { this.age=age; |