public class TextP {
public static void main(String[] args) {
Person p1 = new Person("小红",18);//声明,并附初始值
System.out.println(p1.age+"岁的"+p1.name+"用"+p1.phone1.price+"买"+p1.phone1.color+p1.phone1.type);
Phone phone1=new Phone();//声明
p1.play(phone1);//依赖,调用Phone中的play方法
}
}
public class Person {
//属性
String name;
int age;
Phone phone1=new Phone("白色","OPPO",3999);//关联,作为属性存在
Person(){
}
Person(String name,int age){
this.name=name;
this.age=age;
}
void play(Phone phone) {
phone.call();
phone.internet();
}
}
public class Phone {
String color;
String type;
int price;
Phone(){
}
Phone(String color,String type,int price){
this.color=color;
this.type=type;
this.price=price;
}
void call() {
System.out.println("在打电话");
}
void internet(){
System.out.println("在上网");
}
}