Java笔记 封装、继承
public class Mianbao {
private String color;
private String name;
public void print() {
this.name = "面包";
this.color = "yellow";
System.out.println("姓名:" + this.name + "颜色:" + this.color);
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
}
public class Niunai extends Mianbao {
String type;
public Niunai(String type) {
this.type = type;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
//创建子类 继承父类
public class Niunai extends Mianbao {
String type;
public Niunai(String type) {
this.type = type;
}
//有参构造器
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public static void main(String[] args) {
Niunai nn = new Niunai("液体");
nn.setColor("red");
nn.getColor();
nn.print();
System.out.println(nn.type);
nn.setName("牛奶");
nn.getName();
nn.setColor("baise");
nn.getColor();
nn.setType("liquid");
nn.getType();
System.out.println("名称:" + nn.getName() + "颜色:" + nn.getColor() + "类型:"+ nn.getType());
}
}