1 Star 12 Fork 12

RichardGong / PlayWithCompiler

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ThisSuperTest.java 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
Richard Gong 提交于 2019-09-02 14:25 . added "super" feature
package play;
public class ThisSuperTest {
public static void main(String args[]){
Cow cow = new Cow();
System.out.println();
cow.speak();
}
}
/**
this-and-super.play 面向对象的this和super特性。
*/
class Mammal{
int weight;
public Mammal(){
System.out.println("Mammal() called");
this.weight = 100;
}
Mammal(int weight){
this(); //调用另一个构造函数
System.out.println("Mammal(int weight) called");
this.weight = weight;
this.speak(); //这里多态仍然会生效,实际调用的是Cow的speak方法
}
void speak(){
System.out.println("Mammal speaking...");
}
}
class Cow extends Mammal{
int weight = 300;
public Cow(){
super(200); //调用父类的构造函数
}
void speak(){
System.out.println("in Cow.speak");
System.out.println("super.weight : " + super.weight);
System.out.println("this.weight : " + this.weight);
System.out.println("call super.speak() : " );
super.speak();
System.out.println("Cow speak is : moo~~ moo~~");
}
}
Java
1
https://gitee.com/richard-gong/PlayWithCompiler.git
git@gitee.com:richard-gong/PlayWithCompiler.git
richard-gong
PlayWithCompiler
PlayWithCompiler
master

搜索帮助