编写User类添加属性name,代码如下java">publicclassUser{privateStringname;publicUser(Stringname){this.setName(name);}publicvoidchangeName(Useru){StringuName=u.getName();u.setName(this.name);this.name=uName;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}}其中changeName()用来交换对象的name属性.编写测试类,代码如下:java">publicclassTest{publicstaticvoidmain(Stringargs[]){Userzhangsan=newUser("张三");Userlisi=newUser("李四");zhangsan.changeName(lisi);System.out.println(zhangsan.getName());System.out.println(lisi.getName());}}new出张三李四两个对象,name分别蚂仿为张三李四,调用changeName()方法.输出张三李四的名字,会闷塌纤发现衫皮已经改变~~请采纳,不懂再问
比较简做贺镇单的一个实现方法纯粗:
public class Phone {
private String phone_type;
private String phone_kind;
private String phone_message;
public Phone(String type, String kind, String message){
this.phone_type = type;
this.phone_kind = kind;
this.phone_message = message;
}
public static void main(String[] args){
Phone p = new Phone("Nokia" ,"N75" , "拍裂You have a text message");
System.out.println("phone_type: " + p.phone_type + "\n"
+ "phone_kind: " + p.phone_kind + "\n"
+ "phone_message:" + p.phone_message);
}
}
public class Phone{
private String brand = "HiPhone";
private String type = "山绝逗寨"并让卖;
public void print(){
System.out.print("brand: "滑樱 + brand + " type:" + type);
}
public static void main(String args[]){
new Phone().print();
}
}
public class TestMobile {
public static void main(String args[]){
Mobile mobile=new Mobile("启竖Nokia","答旁搭N97"清拿);
mobile.showInfo();
}
}
全站搜索