“商苑面馆:Java类的继承”的版本间的差异
来自CloudWiki
(创建页面,内容为“Food类: <nowiki> public class Food { //成员变量 //名称 private String name; //价格 private double price; //数量 private int num; /*构造方…”) |
(→main方法里验证) |
||
(未显示同一用户的6个中间版本) | |||
第1行: | 第1行: | ||
+ | ==核心知识点=== | ||
+ | *继承就是子类继承父类的特征和行为,使得代码能够复用; | ||
+ | *声明继承之后,子类就可以使用父类所有的变量和方法; | ||
+ | *如果有变量和方法父类当中不存在,子类可以自己定义。 | ||
+ | |||
+ | ==步骤== | ||
+ | ===定义一个父类=== | ||
+ | 定义一个父类,并把子类当中公共的属性和方法定义到里面: | ||
+ | |||
Food类: | Food类: | ||
− | <nowiki> | + | <nowiki>package entity; |
+ | |||
public class Food { | public class Food { | ||
//成员变量 | //成员变量 | ||
第47行: | 第57行: | ||
public String toString(){ | public String toString(){ | ||
return "名称:" +this.name+"价格:"+ | return "名称:" +this.name+"价格:"+ | ||
− | this.price + " | + | this.price + "剩余数量: " + this.num; |
} | } | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | + | ||
+ | Food f0 = new Food("炸酱面",12,3); | ||
+ | System.out.println(f0.toString()); | ||
+ | |||
} | } | ||
+ | |||
} | } | ||
</nowiki> | </nowiki> | ||
+ | |||
+ | ===子类增添extends关键字=== | ||
+ | |||
+ | <nowiki>public class Noodle extends Food { | ||
+ | ... | ||
+ | }</nowiki> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===写子类的构造方法=== | ||
+ | 子类构造方法 ,调用父类的构造方法。 | ||
+ | |||
+ | <nowiki>public class Noodle extends Food { | ||
+ | private double weight;//份量 | ||
+ | /*构造方法*/ | ||
+ | public Noodle(){ | ||
+ | |||
+ | } | ||
+ | public Noodle(String name,double price,int num){ | ||
+ | super(name,price,num); | ||
+ | } | ||
+ | </nowiki> | ||
+ | ===编写子类里面独有的成员和方法=== | ||
面条类: | 面条类: | ||
第63行: | 第102行: | ||
<nowiki> | <nowiki> | ||
public class Noodle extends Food { | public class Noodle extends Food { | ||
− | + | private double weight;//份量 | |
/*构造方法*/ | /*构造方法*/ | ||
public Noodle(){ | public Noodle(){ | ||
第71行: | 第110行: | ||
super(name,price,num); | super(name,price,num); | ||
} | } | ||
+ | //增添新变量后,新的构造方法 | ||
+ | public Noodle(String name,double price,int num,double weight){ | ||
+ | super(name,price,num); | ||
+ | this.weight = weight; | ||
+ | } | ||
+ | |||
+ | public void getWeight(){ | ||
+ | return this.weight; | ||
+ | |||
+ | } | ||
− | + | }</nowiki> | |
+ | ===main方法里验证=== | ||
+ | 在Noodle类里main方法里写代码,对刚才的编码进行验证。 | ||
+ | |||
+ | 思考题: | ||
+ | |||
+ | *怎样用带参的构造方法创建对象? | ||
+ | |||
+ | |||
+ | <nowiki>public static void main(String[] args) { | ||
// TODO Auto-generated method stub | // TODO Auto-generated method stub | ||
− | Noodle n2 = new Noodle("炸酱面",12,3); | + | Noodle n2 = new Noodle("炸酱面",12,3,500); |
System.out.println("您订的面是"+n2.getName()); | System.out.println("您订的面是"+n2.getName()); | ||
− | System.out.println(" | + | System.out.println("份量:"+n2.getWeight()); |
+ | }</nowiki> | ||
+ | |||
+ | ==父类和子类的完整代码== | ||
+ | |||
+ | |||
+ | Food类: | ||
+ | |||
+ | <nowiki> | ||
+ | package entity; | ||
+ | |||
+ | public class Food { | ||
+ | //成员变量 | ||
+ | //名称 | ||
+ | private String name; | ||
+ | //价格 | ||
+ | private double price; | ||
+ | //数量 | ||
+ | private int num; | ||
+ | |||
+ | /*构造方法*/ | ||
+ | public Food(){ | ||
+ | |||
+ | } | ||
+ | public Food(String name,double price,int num){ | ||
+ | this.name = name; | ||
+ | this.price = price; | ||
+ | this.num = num; | ||
+ | } | ||
+ | |||
+ | //成员方法 | ||
+ | public String getName(){ | ||
+ | return this.name; | ||
+ | } | ||
+ | public double getPrice(){ | ||
+ | return this.price; | ||
+ | } | ||
+ | public int getNum(){ | ||
+ | return this.num; | ||
+ | } | ||
+ | //写方法 | ||
+ | public boolean setName(String s){ | ||
+ | this.name = s; | ||
+ | return true; | ||
+ | } | ||
+ | public boolean setPrice(double p){ | ||
+ | this.price = p; | ||
+ | return true; | ||
+ | } | ||
+ | public boolean setNum(int n){ | ||
+ | this.num = n; | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | public String toString(){ | ||
+ | return "名称:" +this.name+"价格:"+ | ||
+ | this.price + "剩余数量: " + this.num; | ||
} | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Food f0 = new Food("炸酱面",12,3); | ||
+ | System.out.println(f0.toString()); | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
} | } | ||
+ | |||
+ | </nowiki> | ||
+ | |||
+ | 子类:面条类: | ||
+ | |||
+ | <nowiki> | ||
+ | public class Noodle extends Food { | ||
+ | private double weight;//份量 | ||
+ | /*构造方法*/ | ||
+ | public Noodle(){ | ||
+ | |||
+ | } | ||
+ | public Noodle(String name,double price,int num){ | ||
+ | super(name,price,num); | ||
+ | } | ||
+ | //增添新变量后,新的构造方法 | ||
+ | public Noodle(String name,double price,int num,double weight){ | ||
+ | super(name,price,num); | ||
+ | this.weight = weight; | ||
+ | } | ||
+ | |||
+ | public void getWeight(){ | ||
+ | return this.weight; | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | // TODO Auto-generated method stub | ||
+ | |||
+ | Noodle n2 = new Noodle("炸酱面",12,3,500); | ||
+ | System.out.println("您订的面是"+n2.getName()); | ||
+ | System.out.println("份量:"+n2.getWeight()); | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | }</nowiki> | ||
</nowiki> | </nowiki> | ||
− | + | 子类:小吃类: | |
<nowiki> | <nowiki> |
2019年5月20日 (一) 14:07的最新版本
目录
核心知识点=
- 继承就是子类继承父类的特征和行为,使得代码能够复用;
- 声明继承之后,子类就可以使用父类所有的变量和方法;
- 如果有变量和方法父类当中不存在,子类可以自己定义。
步骤
定义一个父类
定义一个父类,并把子类当中公共的属性和方法定义到里面:
Food类:
package entity; public class Food { //成员变量 //名称 private String name; //价格 private double price; //数量 private int num; /*构造方法*/ public Food(){ } public Food(String name,double price,int num){ this.name = name; this.price = price; this.num = num; } //成员方法 public String getName(){ return this.name; } public double getPrice(){ return this.price; } public int getNum(){ return this.num; } //写方法 public boolean setName(String s){ this.name = s; return true; } public boolean setPrice(double p){ this.price = p; return true; } public boolean setNum(int n){ this.num = n; return true; } public String toString(){ return "名称:" +this.name+"价格:"+ this.price + "剩余数量: " + this.num; } public static void main(String[] args) { Food f0 = new Food("炸酱面",12,3); System.out.println(f0.toString()); } }
子类增添extends关键字
public class Noodle extends Food { ... }
写子类的构造方法
子类构造方法 ,调用父类的构造方法。
public class Noodle extends Food { private double weight;//份量 /*构造方法*/ public Noodle(){ } public Noodle(String name,double price,int num){ super(name,price,num); }
编写子类里面独有的成员和方法
面条类:
public class Noodle extends Food { private double weight;//份量 /*构造方法*/ public Noodle(){ } public Noodle(String name,double price,int num){ super(name,price,num); } //增添新变量后,新的构造方法 public Noodle(String name,double price,int num,double weight){ super(name,price,num); this.weight = weight; } public void getWeight(){ return this.weight; } }
main方法里验证
在Noodle类里main方法里写代码,对刚才的编码进行验证。
思考题:
- 怎样用带参的构造方法创建对象?
public static void main(String[] args) { // TODO Auto-generated method stub Noodle n2 = new Noodle("炸酱面",12,3,500); System.out.println("您订的面是"+n2.getName()); System.out.println("份量:"+n2.getWeight()); }
父类和子类的完整代码
Food类:
package entity; public class Food { //成员变量 //名称 private String name; //价格 private double price; //数量 private int num; /*构造方法*/ public Food(){ } public Food(String name,double price,int num){ this.name = name; this.price = price; this.num = num; } //成员方法 public String getName(){ return this.name; } public double getPrice(){ return this.price; } public int getNum(){ return this.num; } //写方法 public boolean setName(String s){ this.name = s; return true; } public boolean setPrice(double p){ this.price = p; return true; } public boolean setNum(int n){ this.num = n; return true; } public String toString(){ return "名称:" +this.name+"价格:"+ this.price + "剩余数量: " + this.num; } public static void main(String[] args) { Food f0 = new Food("炸酱面",12,3); System.out.println(f0.toString()); } }
子类:面条类:
public class Noodle extends Food { private double weight;//份量 /*构造方法*/ public Noodle(){ } public Noodle(String name,double price,int num){ super(name,price,num); } //增添新变量后,新的构造方法 public Noodle(String name,double price,int num,double weight){ super(name,price,num); this.weight = weight; } public void getWeight(){ return this.weight; } public static void main(String[] args) { // TODO Auto-generated method stub Noodle n2 = new Noodle("炸酱面",12,3,500); System.out.println("您订的面是"+n2.getName()); System.out.println("份量:"+n2.getWeight()); } }
</nowiki>
子类:小吃类:
public class SmallEat extends Food{ private String flavor; SmallEat(){ } SmallEat(String name,double price,int num){ super(name,price,num); } public String toString(){ return "名称:" + getName()+"价格:"+ getPrice() + "数量: " + getNum() + "口味:"+ flavor; } public static void main(String[] args) { // TODO Auto-generated method stub SmallEat s = new SmallEat("鹌鹑蛋",3,2); System.out.println(s.toString()); } }