文章标题

来源:互联网 发布:ssm slf4j sql 编辑:程序博客网 时间:2024/06/07 01:16

封装
class Hua
{
private String name; //花的属性
private String color;
private String location;
public void setname(String name){ // 设置花对象的属性
this.name=name;
}
public void setcolor(String color){
this.color=color;
}
public void setlocation(String location){
this.location=location;
}
public String gatname(){ //访问器获得花属性
return name;
}
public String gatcolor(){
return color;
}
public String gatlocation(){
return location;
}
private void print(){
System.out.println(“这就是”+name);
}
public String tostring(){
String information=(“花名;”+name+”;”+” “+”花的颜色;”+color1+”;”+”“+”花的出产地;”+location);
print();
return information;
}

}

class Huatext
{
public static void main(String args){
flower f1=new flower(); //创建三个对象f1,f2,f3
flower f2=new flower();
flower f3=new flower();
f1.setname(“牡丹”);
f1.setcolor(“红色”);
f1.setlocation(“云南”);
f2.setname(“玫瑰”);
f2.setcolor(“黄色”);
f2.setlocation(“北京”);
f3.setname(“月季”);
f3.setcolor(“蓝色”);
f3.setlocation(“上海”);
//输出相应的信息
System.out.println(f1.tostring());
System.out.println(f2.tostring());
System.out.println(f3.tostring());
}
}
请各位大神们多多指教!!

0 0