构造方法,super,this
来源:互联网 发布:网络监控头 接usb 编辑:程序博客网 时间:2024/05/01 00:54
父类
public class Animal { public String nameAnimal = "动物"; Animal(){ System.out.println("我是父类Animal的构造方法,不带参"); } Animal(String str){ System.out.println("我是父类Animal的构造方法,"+str); } protected void eat(){ System.out.println("我是父类的eat方法--我是"+nameAnimal+",我是protected修饰的方法"); }}
子类
public class Dog extends Animal { Dog(){ // 默认执行super(); //super 一般放在第一行 super.eat(); System.out.println("我是Dog的构造方法"); this.eat(); } public void eat(){ System.out.println("我覆盖了父类的eat方法"); }}
测试方法
public class test { public static void main(String[] args) { Dog dog = new Dog(); Animal a= new Animal("带参"); }}
结果
我是父类Animal的构造方法,不带参
我是父类的eat方法–我是动物,我是protected修饰的方法
我是Dog的构造方法
我覆盖了父类的eat方法
我是父类Animal的构造方法,带参
0 0
- 构造方法,super,this
- [java]构造方法super() this()
- 构造,super,this
- 构造函数,super,this
- super() this() super this 与构造函数
- 使用Java的子类调用构造方法super()和this()
- 构造方法的调用并涉及this、super用法
- java中this用法 super()用法 以及构造方法
- [Java] super,this 的区别及构造方法的使用
- java继承中的构造方法及this,super的使用
- 【Java概念】构造方法、this、super(6)
- day12 this super 在构造方法中的使用
- super,this引用构造函数
- 构造方法 -- super()方法
- super()和this()方法
- this()与super()之构造器
- 继承,super,this关键字及构造器
- 黑马程序员_五 【初识面向对象】【类、构造方法、对象】【封装、继承、多态】【this、super】
- Eclipse中设置在创建新类时自动生成注释
- 1.4 linux安装mysql5.7.11
- Activity简单几步支持向右滑动返回
- Linux tee --重定向输出到多个文件
- openssl 升级
- 构造方法,super,this
- xoyojank 的 UE4中的DynamicTexture数据更新
- 机器学习:集成学习
- 根据页面edittext的内容控制按钮可点击状态
- 安卓开发设置Tabhost位于底部的方法
- linux输入子系统之按键驱动
- Eclipse打开之后卡住
- 信号量Semaphore的使用Demo
- android 之listview(一)