美文网首页Java4Android
Twleve Day(面向对象之继承II)

Twleve Day(面向对象之继承II)

作者: 白如白牙 | 来源:发表于2016-03-28 12:09 被阅读6次
  1. 函数的复写(override)
    2.使用super调用父类的成员函数
    3.使用super调用父类的构造函数

比较super和this特别相似。

  • 对于this而言,this()里面加上参数就可以调用本类中的构造函数,而this加上. 加上函数名就可以调用本类中的成员函数

对于super而言,super()里面加上参数就可以调用父类中的构造函数,而super加上. 加上函数名就可以调用父类中的成员函数
eg:super.introduce();

程序员:懒是其必要的优良品质之一,越懒惰,重复代码越少,但是手可以懒,脑子一定要勤快。

javac*.java可以一键编译一个文件夹下面的所有.java的文档,

一个类:重载
两个类:复写(override)

————————————————————————————————————————

代码:

class person{
String name;
int age;
void introduce(){
System.out.println("我的姓名是"+name+",我的年龄是"+age);
}
}
————————————————————————————————————————
//复写(override)也被称为覆盖或者重写
//1.在具有父子关系的两个类中
//2.父类和子类各有一个函数,这两个函数的定义(返回值类型。函数名和参数列表)完全相同
class student extends person{

String  address;
void introduce(){
    super.introduce();//System.out.println("我的姓名是"+name+",我的年龄是"+age);
    System.out.println("我的家在"+address);
    
}

}
————————————————————————————————————————
class test{
public static void main(String args[]){
student a = new student();
a.name = "zhangshan";
a.age =20;
a.address="tianmen";
a.introduce();
person p = new person();
p.name ="lisi";
p.age= 10;
p.introduce();
}
}

相关文章

网友评论

    本文标题:Twleve Day(面向对象之继承II)

    本文链接:https://www.haomeiwen.com/subject/gdyjlttx.html