java基本概念

继承

java是单继承,一个儿子只能由一个父亲。子类继承父类,则子类可以调用父类非私有的变量和方法。子类不继承父类的构造方法。

在子类构造方法中用super调用父类的构造方法。

如果子类的构造方法中没有显示地调用父类构造方法,也没有使用this关键字调用重载的其它构造方法,则系统默认调用父类无参数的构造方法。

如果子类的构造方法没有显式调用父类的构造方法,父类又没有无参构造方法,则编译错误。

子类转父类,自动转

父类转子类必须先向下转型再强制上转。

重载:方法名相同,方法的参数个数或参数类型不同!返回值类型可以相同,也可以不同。

重写:父类与子类多态性的表现,一般是子类重写父类的方法,方法名,参数列表和返回值类型相同。

 

子类:

java基本概念
 1 public class Child extends Father{
 2     public int age;
 3     
 4     private String name;
 5     
 6     public Child(){
 7         super();
 8     }
 9     public Child(int age){
10         this.age=age;
11     }
12     public void methodA(){
13         System.out.println("c method");
14     }
15     public void methodB(){
16         System.out.println("b method");
17     }
18     public void methodC(){
19         System.out.println("c method");
20     }
21     public int methodD(int m,int n){
22         System.out.println("c methodD");
23         return m+n;
24     }
25 }
java基本概念

父类:

java基本概念
 1 public class Father {
 2     public int age;
 3     
 4     private String name;
 5     
 6     public Father(){}
 7     
 8     public Father(int age){
 9         this.age=age;
10     }
11     public Father(int age,String name){
12         this(age);
13         this.name=name;
14     }
15     public void methodA(){
16         System.out.println("F methodA");
17     }
18     private void methodB(){
19         System.out.println("F methodB");
20     }
21     public void methodC(int i){
22         System.out.println("f i:"+i);
23     }
24     public int methodD(int i,int j){
25         return i+j;
26     }
27 }
java基本概念

测试类:

java基本概念
public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        c.methodA();
        Father f = new Father();
        f.methodA();

        c.age = 100;
        System.out.println(c.age);
        // System.out.println(c.name);//错,私有的

        System.out.println(f.age);// 0

        
        System.out.println(c.age);// 100
        System.out.println(f.age);// 0
        // 父类引用指向子类对象 1满足覆盖条件的方法,只存在子类的方法
        // 2非私有的同名变量,隐藏关系,子类变量和父类都存在
        // 变量的值取决于引用的类型,如果引用的类型是father,那么调用父类的变量,如果是子类,则调用子类的变量
        c.methodB();// b
        f = c;
        f.methodA();// c
    }

}
java基本概念

 抽象类和接口

包含了抽象方法的类就是抽象类,抽象方法只有方法声明没有方法体。抽象类也可以被继承但不能被实例化,主要是提高方法的复用性。

java基本概念
public abstract class TestAbstract {

    //描述方法的作用
    public abstract void method();
    
    public void methodA(){}
}
class Child extends TestAbstract{
    //注解 如果形成此关系,注解有效,否则报错
    //这里为标识该方法是否覆盖了父类的方法
    //描述方法的实现细节
    @Override
    public void method(){
        System.out.println("aaa");
    }
    
    public static void main(String[] args) {
        TestAbstract ta=new Child();
        ta.method();
    }
java基本概念

接口是一系列方法的声明的集合,Java接口不能有构造器,可以有public,static和final的属性。

java基本概念
public interface TestInterface {
    public void method();
    
    public void methodB();
    
}
 class TestInterfaceImpl implements TestInterface{

    @Override
    public void method() {
        
        System.out.println("to do...");
    }

    @Override
    public void methodB() {
        System.out.println("=====");
    }
    public static void main(String[] args){
        TestInterface ti=new TestInterfaceImpl();
        ti.method();
    }
    
}
java基本概念

 

java基本概念,布布扣,bubuko.com

java基本概念

上一篇:java的IO操作


下一篇:Java学习笔记-策略模式