class 父类 {
}
class 子类 extends 父类 {
}
继承的特性:
public interface A {
public void eat();
public void sleep();
}
public interface B {
public void show();
}
public class C implements A,B {
}
class Animal {
void eat() {
System.out.println("animal : eat");
}
}
class Dog extends Animal {
void eat() {
System.out.println("dog : eat");
}
void eatTest() {
this.eat(); // this 调用自己的方法
super.eat(); // super 调用父类方法
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Animal();
a.eat();
Dog d = new Dog();
d.eatTest();
}
}
/**
* @author wuming
* @date 2023-12-20 13:48
* @description
*/
class SuperClass{
private int n;
SuperClass(){
System.out.println("SuperClass 无参构造");
}
SuperClass(int n){
System.out.println("SuperClass 有参构造" + n);
this.n = n;
}
}
class SubClass extends SuperClass{
private int n;
// 系统自动调用父类的无参构造函数
SubClass(){
System.out.println("SubClass 隐式调用父类无参构造");
}
SubClass(int n){
super(n);
System.out.println("SubClass 显式调用父类有参构造");
this.n = n;
}
}
public class ExtendTest {
public static void main (String args[]){
new SubClass();
new SubClass(200);
}
}
SuperClass 无参构造
SubClass 隐式调用父类无参构造
SuperClass 有参构造200
SubClass 显式调用父类有参构造