[笔记]java-继承

extends 关键字

super 关键字

如果class没有显示的构造函数,则自动创建构造函数,如果显示创建,则不创建默认构造函数。

如果子类class没有调用基类构造函数,则调用默认构造函数。

同 c++,不要在构造函数中调用动态绑定的方法(c++中的虚函数),动态绑定时,基类对象不可调用子类方法

class test1{
      public test1(int i){
          System.out.println("init test1");
      }
      public test1(){
          
      }
}
class test2 extends test1{
  
      public test2(int i){
//默认调用test1(),如果 test1没有定义test1(),则报错。可显示调用test1(int i)
          System.out.println("init test2");
      }
}

与c++不同,c++中,如果子类由与基类相同方法名,则复写或者重写,而java不会覆盖基类同名方法,亦可调用

class test1{
    
    static {
        System.out.println("static test1");
    }
      {
        System.out.println("no static test1");
    }
      public test1(int i){
          System.out.println("init test1");
      }
      public test1(){
          
      }
}
class test2 extends test1{
    static {
        System.out.println("static test2");
    }
      {
        System.out.println("no static test2");
    }
      public test2(int i){
          System.out.println("init test2");
      }
}

final关键字

final数据:编译器为常量,如果数据为一个对象,则意味着不能指向别的对象,但是指向的对象本身是可以被修改的。

final参数:相当于c++中const,不可改变参数

final方法:不可被覆写(子类中,但是可以重载),具有inline功能,所有private方法默认为final

final类:禁止继承

多态

abstract类:抽象类,类似c++中virtual类

abstract class a{
public abstract void foo(); //如果不定义方法,则方法必须有abstract
}

interface接口:

接口可以继承接口

interface DangerousMonster extends Monster { void destroy();

包含了基本数 据类型的数据成员,但它们都默认为static 和final,只能声明,不能定义方法,方法默认为 public 

多重继承:java中没有多重继承,但是可以有多个接口

class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly {
public void swim() {}
public void fly() {}
 }

内部类:

若想在除外部类非 static 方法内部之外的任何地方生成内部类的一个对象,必须将那个对象的类型设为“外 部类名.

只有内部类才能被设为private,不被其他类访问,隐藏细节

//: Parcel2.java
// Returning a handle to an inner class package c07.parcel2;
public class Parcel2 { class Contents {
private int i = 11;
public int value() { return i; } }
class Destination { private String label;Destination(String whereTo) { label = whereTo;
} String readLabel() {
return label; } } public Destination to(String s) { return new Destination(s); } public Contents cont() { return new Contents();
}
public void ship(String dest) {
Contents c = cont(); Destination d = to(dest); } public static void main(String[] args) { Parcel2 p = new Parcel2();
p.ship("Tanzania"); Parcel2 q = new Parcel2(); // Defining handles to inner classes:
Parcel2.Contents c = q.cont();
Parcel2.Destination d = q.to("Borneo");
} } ///:~

方法中的内部类

出了作用域则不可访问类名,但是仍可返回对象,使用动态(继承的接口或基类),可访问外部类,与c++不同

匿名类:无构造函数,可使用初始化{}构造

return new Contents() {
private int i = 11;
public int value() { return i; } };
//这种奇怪的语法要表达的意思是:“创建从 Contents 衍生出来的匿名类的一个对象”。由 new 表达式返回的 句柄会自动上溯造型成一个Contents 句柄。匿名内部类的语法其实要表达的是:
class MyContents extends Contents { private int i = 11;
public int value() { return i; }
}
return new MyContents();

外部初始化内部类对象:非static内部类须通过外部类对象初始化

//: Parcel11.java
// Creating inner classes package c07.parcel11;
public class Parcel11 { class Contents {
private int i = 11;
public int value() { return i; } }
class Destination {
private String label; Destination(String whereTo) {
label = whereTo; }
String readLabel() { return label; } }
public static void main(String[] args) { Parcel11 p = new Parcel11();
// Must use instance of outer class
// to create an instances of the inner class: Parcel11.Contents c = p.new Contents(); Parcel11.Destination d =
p.new Destination("Tanzania"); }
//除非已拥有外部类的一个对象,否则不可能创建内部类的一个对象。这是由于内部类的对象已同创建 它的外部类的对象“默默”地连接到一起。然而,如果生成一个static 内部类,就不需要指向外部类对象的 一个句柄。
} ///:~
原文地址:https://www.cnblogs.com/zengyou/p/2772197.html