编程语言
首页 > 编程语言> > Java 基础 2.3 (多肽,抽象,接口)

Java 基础 2.3 (多肽,抽象,接口)

作者:互联网

多肽

Children

package Polypeptide;

public class Children extends Father {
    public void exit(){
        System.out.println("你儿子16了");
    }
    public void girl(){
        System.out.println("你女子12了");
    }
}

Father

package Polypeptide;

/**
 * 多肽
 */
public class Father {
    public void exit(){
        System.out.println("你爸爸56了");
    }
}

Instance 

package Polypeptide;

public class Instance {
    public static void main(String[] args) {
        /*
//      引用多肽 既可以引用父类也可以引用子类
        Father far = new Father();
        Children chil = new Children();
        Sister sis = new Sister();
//        方法多肽
        far.exit();
        chil.exit();
        sis.exit();
        chil.girl();
        */

//        引用类型转换
        /*
        Children chil = new Children();
        Father far = chil; //向上类型转换,自动转换类型
//        far.exit();
        Children chil2 = (Children)far; //指向的还是 Children;
//        chil2.exit();
        if(far instanceof Sister){ //判断是否可以转换
            Sister sis = (Sister)far;
            sis.exit();
        }else {
            System.out.println("无法转换 Father");
        }
        */

    }
}

Sister

package Polypeptide;

public class Sister extends Father{
}

抽象类 abstract

Father

package Abstract;

/**
 * 抽象类 abstract
 * 相当于定义了一个 类的模板
 */
public abstract class Father {

    //抽象方法没有方法体
    public abstract void call();
    public abstract void message();


}

Children

package Abstract;

public class Children extends Father
{
    @Override
    public void call(){
        System.out.println("一个");
    }

    @Override
    public void message(){
        System.out.println("二个");
    }
}

Instance

package Abstract;

public class Instance {
    public static void main(String[] args) {
        Children chil = new Children();
        chil.call();
    }
}

接口类

Relatives

package Interface;

/**
 * 接口类
 */
public interface Relatives {
    public void relatives();
}

Father 

package Interface;

public abstract class Father {
    public abstract void call();
    public abstract void message();
}

Children

package Interface;

public class Children extends Father implements Relatives
{
    @Override
    public void call(){
        System.out.println("一个");
    }

    @Override
    public void message(){
        System.out.println("二个");
    }

    @Override
    public void relatives() {
        System.out.println("接口");
    }
}

Sister

package Interface;

public class Sister implements Relatives {
    @Override
    public void relatives() {
        System.out.println("有了接口了");
    }
}

Instance 

package Interface;


public class Instance {
    public static void main(String[] args) {
        Children chil = new Children();
        chil.call();
        chil.relatives();

        Sister sis = new Sister();
        sis.relatives();


        //匿名内部类 1
        Relatives rel = new Relatives() {
            @Override
            public void relatives() {
                System.out.println("哈哈");
            }
        }; //分号不能丢

        rel.relatives();


        //匿名内部类 2
        new Relatives() {
            @Override
            public void relatives() {
                System.out.println("哈哈哈哈");
            }
        }.relatives();
    }
}

 

标签:Java,void,Father,System,Children,多肽,2.3,public,out
来源: https://blog.csdn.net/ListFish/article/details/89455053