java接口学习笔记

interface Animal{
	int ID=1;
	void breathe();
	default void getType(String type){
		System.out.println("该动物属于:"+type);
	}
	static int getID(){
		return Animal.ID;
	}
}
class Dog implements Animal{
	public void breathe(){
		System.out.println("狗在呼吸");
	}
}
public class Example1{
	public static void main(String args[]){
		System,out.pritnln(Animal.getID());
		Dog dog=new Dog();
		System.out.println(dog.ID);
		dog.breathe();
		dog.getType("犬科");
	}
}

运行结果:
1
1
狗在呼吸
该动物属于:犬科

public class HelloWorld {
    public static void main(String []args) {
        f f1= new g();
        f1.s();
    }
}
interface f{
    public void s();
}
class g implements f{
    public void s(){
        System.out.println("tyu");
    }
}

运行结果:
tyu

改成如下代码是报错

在这里插入图片描述

public class HelloWorld {
    public static void main(String []args) {
        f f1= new f() {
            @Override
            public void s() {
                System.out.println("1");
            }
        };
        f1.s();
    }
}
interface f{
    public void s();
}
class g implements f{
    public void s(){
        System.out.println("tyu");
    }
}

运行结果:
1

interface s{
    public void g();
}
class d implements s{
    public void g(){
        System.out.println("1");
    }
}
public class HelloWorld{
    s s1=new d();
    public void ghj(){
        s1.g();
    }

    public static void main(String[] args) {
        HelloWorld helloWorld=new HelloWorld();
        helloWorld.ghj();
    }
}

运行结果:
1

interface s{
    public void g();
}
class d implements s{
    private static d s1=new d();
    public void g(){
        System.out.println("1");
    }
    public static void fgh(){
        s1.g();
    }
}
public class HelloWorld{
    public static void main(String[] args) {
        d.fgh();
        System.out.println("16890");
    }
}

运行结果:
1
16890

interface s{
    public void g();
}
class d implements s{
    static s s1=new d();
    public void g(){
        System.out.println("156");
    }
    public static void k(){
        s1.g();
    }
}
public class HelloWorld{
    public static void main(String[] args) {
        d.k();
        System.out.println("16890");
    }
}

运行结果:
156
16890

interface s{
    public void g();
}
class d implements s{
    s s1= new s() {
        @Override
        public void g() {
            System.out.println("1569");
        }
    };
    public void g(){
        System.out.println("156");
    }
    public void k(){
        s1.g();
    }
}
public class HelloWorld{
    public static void main(String[] args) {
        d d1=new d();
        d1.k();
        System.out.println("16890");
    }
}

运行结果:
1569
16890

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值