java中super关键字测试总结
2017-12-12 22:50
295 查看
public class SuperStu {
/*super:指向当前对象的父类资源
*1.调用父类构造器(该语句只能写在构造器第一行)
*2.调用父类属性(用于和子类同名的属性区分)
*3.调用父类的方法;
*注意点:
*1.this可以作为一个单独的引用类型变量使用;
*2.super只是一个指向父类资源的指针,不能单独使用
*
*/
public static void main(String[] args)
{
WaterFlower wf = new WaterFlower("水仙花","绿色",15,false,'无');
wf.type = "大葱";
System.out.println(wf.type);
System.out.println(wf.getType());
wf.makeMyself();
}
public static void makeFood(WaterFlower wf){
System.out.println("将"+wf.type+"做成"+wf.type+"炒肉");
}
public static void makeFood(Flower wf){
System.out.println("将"+wf.type+"做成"+wf.type+"炒肉");
}
}
class WaterFlower extends Flower{
String type = "葱";
public WaterFlower(){}
public WaterFlower(String type, String color, double size, boolean colorFul, char smile){
//this();
super(type,color,size,colorFul,smile);
//this();
}
public String getType(){
return super.type;
}
//将自己做成菜
public void makeMyself(){
/*WaterFlower wf = new WaterFlower();
wf.type = "韭菜";*/
Flower flo = new Flower();
flo.type = "西蓝花";
//SuperStu.makeFood(super);super不能单独作为一个引用类型变量使用
}
}
更多java知识请访问:How2J 的 Java教程
/*super:指向当前对象的父类资源
*1.调用父类构造器(该语句只能写在构造器第一行)
*2.调用父类属性(用于和子类同名的属性区分)
*3.调用父类的方法;
*注意点:
*1.this可以作为一个单独的引用类型变量使用;
*2.super只是一个指向父类资源的指针,不能单独使用
*
*/
public static void main(String[] args)
{
WaterFlower wf = new WaterFlower("水仙花","绿色",15,false,'无');
wf.type = "大葱";
System.out.println(wf.type);
System.out.println(wf.getType());
wf.makeMyself();
}
public static void makeFood(WaterFlower wf){
System.out.println("将"+wf.type+"做成"+wf.type+"炒肉");
}
public static void makeFood(Flower wf){
System.out.println("将"+wf.type+"做成"+wf.type+"炒肉");
}
}
class WaterFlower extends Flower{
String type = "葱";
public WaterFlower(){}
public WaterFlower(String type, String color, double size, boolean colorFul, char smile){
//this();
super(type,color,size,colorFul,smile);
//this();
}
public String getType(){
return super.type;
}
//将自己做成菜
public void makeMyself(){
/*WaterFlower wf = new WaterFlower();
wf.type = "韭菜";*/
Flower flo = new Flower();
flo.type = "西蓝花";
//SuperStu.makeFood(super);super不能单独作为一个引用类型变量使用
}
}
更多java知识请访问:How2J 的 Java教程
相关文章推荐
- Java关键字this、super使用总结
- Java关键字this、super使用总结(转)
- Java关键字this,super使用总结
- Java关键字this、super使用总结
- Java关键字this、super使用总结
- Java关键字this、super使用总结
- Java关键字this、super使用总结
- Java关键字this、super使用总结
- java--关键字this、super使用总结
- Java super关键字总结
- Java关键字this、super使用总结
- Java关键字this、super使用总结
- Java关键字this、super使用总结
- Java关键字this、super使用总结
- Java关键字this、super使用总结
- Java关键字this、super使用总结
- Java关键字this、super使用总结
- Java关键字final、static、this、super使用总结
- Java 关键字 this super 使用总结