Java-抽象类与接口

在这里插入图片描述
在这里插入图片描述

package cn.ALAN_CF.studyf007;
/**
 * @author 15328
 */
public class Main {
    public static void main(String[] args) {
        extendsFoo stu0 = new extendsFoo("Alan_cf");
        System.out.println("the name is : "+ stu0.getName());
        stu0.run();
        
        Student stu1 = new Student();
        stu1.greeting();
        Foobar stu2 = new Foobar();
        stu2.x();
        stu2.greeting();
        stu2.run();
        
        Stu xx=new Stu() {
            @Override
            public void run() {
                
            }
            
            @Override
            public void greeting() {
                
            }
        };
        xx.x();
    }
}
abstract class Foo{
    /**对一个抽象类来说,里面至少要存在一个抽象方法
     * 然而
     *  接口:  1、由只有抽象方法或static方法、没有字段的抽象类封装而成
     *          2、可以有常量(final)字段或者static静态字段
     *          3、非抽象方法只能用default修饰
     * */
    protected String name;
    public String getName(){
       return this.name;
    }
    public Foo(String name){
        this.name = name;
    }
    public abstract void run();
}
class extendsFoo extends Foo{

    public extendsFoo(String name){
        super(name);
    }

    @Override
    public void run(){
        System.out.println("This is the  run method from the extendsFoo extends from Foo ");
    }
}
/**abstract class Person{
    public abstract void take();
    public abstract void run();
}*/
interface Person{
    void greeting();
}

interface Stu extends Person{
    /**接口本身是个抽象类,也可以有接口与接口直接的继承关系*/
    void run();
    public default void x(){
        System.out.println("This is the default method : X    ***** from the interface : Stu");
    }
}

class Student implements Person{
    @Override
    public void greeting(){
        System.out.println("This is the first greeting, which is the method of the Student class");
    }
}

class Foobar implements Stu{
    /**使用了接口的非抽象类,
     * 要对接口声明过的所有方法进行覆写*/
    @Override
    public void run() {
        System.out.println("This is the second method : RUN    ***** from the class Foobar");
    }

    @Override
    public void greeting() {
        System.out.println("This is the second method : greeting    ***** from the class Foobar");
    }
}


在这里插入图片描述

package cn.ALAN_CF.studyf007;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author 15328
 * List是个接口,是个抽象的类,不能实例化
 * 利用多态,List引用ArrayList
 * ArrayList 是抽象类的一个子类,这个抽象子类又实现了List
 * 接口实际上是个类
 * 用接口实现的多重继承,相当于继承自多个父类,
 * 在java中父类只能有一个,但是接口可以有很多个
 * 将方法抽象到接口中
 */
public class Xain {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        for(Object x : list){
            System.out.println(x);
        }
        ClassA a = new ClassA();
        a.run();
    }
}
interface InerfaceA{
    /**             抽象类         接口
     *  inherit    extends      implements
     *  字段        可以有     只有static和final
     *  非抽象方法   可以有     使用default方法
     *  */
    default void run(){
        System.out.println("Here is a Interface");
    }
}
class ClassA implements InerfaceA{

}

在这里插入图片描述

posted @   在天边偷看小天使  阅读(1)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示