————————————Mobile_Phone类————————————————————————————————
package demo;//手机类
public class Mobile_Phone{
//构造方法
Mobile_Phone(){
}
//构造方法
Mobile_Phone(String _type,float _price,String _color,String _peoName){
this.peoName=_peoName;
this.type=_type;
this.price=_price;
this.color=_color;
}
//手机属性,价格,种类,主人,颜色
float price;
String type;
String peoName;
String color;
}—————————————人类————————————————————————————————
package demo;
public class people{
//
int age;
String name;
Mobile_Phone myPhone;
String hobby;
people(){
}
people(String _name,int _age,String _hobby,Mobile_Phone _myPhone){
name=_name;
age=_age;
hobby=_hobby;
myPhone=_myPhone;
}
//购买手机的方法,生成一个手机对象
public void buyphone(String _type,float _price,String _color){
Mobile_Phone _myphone=new Mobile_Phone(_type,_price,_color,this.name);
}
//一个显示个人信息的方法
public void show(){
System.out.println("_______________________________________________");
System.out.println("我的名字叫"+this.name+"\n");
System.out.println("我的年龄是"+this.age+"\n");
System.out.println("我的爱好是"+this.hobby+"\n");
//下面就是有问题的地方
System.out.println("我的手机是"+this.myPhone.type+"颜色是"
+this.myPhone.color+"花了我"+this.myPhone.price+"的钱/t");
}
}
——————————————测试类———————————————————————————————
package demo;
public class test{
/*
*程序入口
*/
public static void main(String []args){
people jack=new people();
jack.name="Jack";
jack.age=19;
jack.hobby="跑步";
jack.buyphone("sony",500,"yellow");
//这里是异常
jack.show(); }
} 程序没有语法错误,不过会报异常,应该是我理解错了吧,请大家帮帮我吧,谢谢!!
package demo;//手机类
public class Mobile_Phone{
//构造方法
Mobile_Phone(){
}
//构造方法
Mobile_Phone(String _type,float _price,String _color,String _peoName){
this.peoName=_peoName;
this.type=_type;
this.price=_price;
this.color=_color;
}
//手机属性,价格,种类,主人,颜色
float price;
String type;
String peoName;
String color;
}—————————————人类————————————————————————————————
package demo;
public class people{
//
int age;
String name;
Mobile_Phone myPhone;
String hobby;
people(){
}
people(String _name,int _age,String _hobby,Mobile_Phone _myPhone){
name=_name;
age=_age;
hobby=_hobby;
myPhone=_myPhone;
}
//购买手机的方法,生成一个手机对象
public void buyphone(String _type,float _price,String _color){
Mobile_Phone _myphone=new Mobile_Phone(_type,_price,_color,this.name);
}
//一个显示个人信息的方法
public void show(){
System.out.println("_______________________________________________");
System.out.println("我的名字叫"+this.name+"\n");
System.out.println("我的年龄是"+this.age+"\n");
System.out.println("我的爱好是"+this.hobby+"\n");
//下面就是有问题的地方
System.out.println("我的手机是"+this.myPhone.type+"颜色是"
+this.myPhone.color+"花了我"+this.myPhone.price+"的钱/t");
}
}
——————————————测试类———————————————————————————————
package demo;
public class test{
/*
*程序入口
*/
public static void main(String []args){
people jack=new people();
jack.name="Jack";
jack.age=19;
jack.hobby="跑步";
jack.buyphone("sony",500,"yellow");
//这里是异常
jack.show(); }
} 程序没有语法错误,不过会报异常,应该是我理解错了吧,请大家帮帮我吧,谢谢!!
this.myPhone.color
this.myPhone.price不是静态变量能这样用??
Mobile_Phone _myphone = new Mobile_Phone(_type, _price, _color, this.name);}