022.static与final关键字详解

第二十二讲

  1. 接口(interface):接口的地位等同于class, 接口中所有方法都是抽象方法。 在声明接口中的方法时,可以使用abstract关键字,也可以不使用。通常情况下,都会省略掉abstract关键字。(有声明,无实现)
package exercise;

public interface exercise003 {
    public void output();//抽象方法
    public static void main(String[] args){
    }
}

在这里插入图片描述
2. 可以将接口看作是特殊的抽象类(抽象类中可以有具体方法,也可以有抽象方法,而接口中只能有抽象方法,不能有具体方法)。
3. 类可以实现接口,实现使用关键字implements表示,代表了某个类实现了某个接口。

package exercise;
public class exercise003 {
    public static void main (String[] args){
        Myclass myclass = new Myclass();
        myclass.output();
    }
}
interface MyInterface{
    public void output();
}
class Myclass implements MyInterface{
     public void output(){
         System.out.println("output");
     }
}

在这里插入图片描述
4. 一个类实现了某个接口,那么该类必须要实现接口声明的所有方法。如果该类是个抽象类,那么就无需实现接口中的方法了。
5. Java是单继承的,也就是说某个类只能有唯一一个父类;一个类可以实现多个接口,多个接口之间使用逗号分隔。

package exercise;

public class exercise003 {
    public static void main (String[] args){
        Myclass myclass = new Myclass();
        myclass.output();
        myclass.output2();
    }
}
interface MyInterface{
    public void output();
}
interface MyInterface2{
    public void output2();
}
class Myclass implements MyInterface,MyInterface2{
     public void output(){
         System.out.println("output");
     }

    @Override
    public void output2() {
        System.out.println("output2");
    }
}

在这里插入图片描述

package exercise;

public class exercise003 {
    public static void main (String[] args){
        Myclass myclass = new Myclass();
        myclass.output();
        myclass.output2();
        myclass.output3();
    }
}
interface MyInterface{
    public void output();
}
interface MyInterface2{
    public void output2();
}
class Myparent{
    public void output3(){
        System.out.println("output3");
    }
}
class Myclass extends Myparent implements MyInterface,MyInterface2{
     public void output(){
         System.out.println("output");
     }

    @Override
    public void output2() {
        System.out.println("output2");
    }
}

在这里插入图片描述
6. 多态:所谓多态,就是父类型的引用可以指向子类型的对象,或者接口类型的引用可以指向实现该接口的类的实例。关于接口与实现接口的类之间的强制类型转换方式与父类和子类之间的强制类型转换方式完全一样。

package exercise;

public class exercise004 {
    public static void mian (String[] args){
        AA bb = new BB();//多态
        bb.output();
    }
}
interface  AA{
    public void output();
}
class BB implements AA{
    public void output(){
        System.out.println("BB");
    }
}

在这里插入图片描述
7. Static 关键字:可以用于修饰属性,也可以用于修饰方法,还可以用于修饰类。
8. static 修饰属性:无论一个类生成了多少个对象,所有这些对象共同使用唯一一份静态成员变量;一个对象对该静态成员变量进行了修改,其他对象的该静态成员变量的值也随之发生变化。如果一个成员变量是static的,那么我们可以通过 类名.成员变量名 的方式来使用它(Java推荐我们使用这种方法)。

package exercise;

public class exercise005 {
    public static void main (String[] args){
        MyStatic myStatic = new MyStatic();
        MyStatic myStatic1 = new MyStatic();
        myStatic.a = 10;
        System.out.println(myStatic1.a);
    }
}
class MyStatic{
    static int a;
}

在这里插入图片描述

package exercise;
public class exercise005 {
    public static void main (String[] args){
        MyStatic myStatic = new MyStatic();
        myStatic.a = 10;
        System.out.println(myStatic.a);
    }
}
class MyStatic{
    static int a;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值