java基础:java之多态和内部类

多态

多态就是指不同类继承了同一个父类的同时,在调用同一个方法的时候可以使得呈现出不同具体的方法效果。

java的多态由它的继承和重写所决定。

abstract class Animal{
    abstract void shout();
}
class Cat extends Animal{
    public void shout(){
        System.out.println("喵喵喵");
    }
}

class Dog extends Animal{
    public void shout(){
        System.out.println("汪汪汪");
    }
}
public class Example001 {
    public static void main(String[] args) {
        Animal a1 = new Cat();
        Animal a2 = new Dog();
        a1.shout();
        a2.shout();
    }
}

对象的类型转换

上述的代码中本质就是向上转型就是实现了Animal类的具体动物特例,同样我们也可以向下转型。也就是说向上转型中创立的对象本质还是Animal的对象,所以也就不可以调用的子类中的方法,必须用向下转型来实现。

interface Animal{
     void shout();
}
class Cat implements Animal{
    public void shout(){
        System.out.println("喵喵喵");
    }

    public void catchmouse(){
        System.out.println("猫抓老鼠");
    }

}
public class Example001 {
    public static void main(String[] args) {
        Animal a1 = new Cat();
        Cat cat = (Cat)a1;//转型的功能
        cat.shout();
        cat.catchmouse();
    }
}

但是如果我们判断错了对象和类的所属关系,程序就会报错,所以我们我可以使用instanceof这个关键字判定是否是其所属关系。

内部类

成员内部类

在成员内部类中,我们对于内外类而言,双方可以互相访问并不限制。

class Outer{
    int m = 0;
    public void test1(){
        System.out.println("外部成员类方法");
    }
    class Inner{
        int n=1;
        void test2(){
            System.out.println("外部成员方法m="+m);
            test1();
        }
        void show1(){
            System.out.println("内部类成员方法");
        }

        }
        void show2(){
        Inner inner = new Inner();
            System.out.println("内部类成员方法n="+inner.n);
            inner.show1();
        }
    }
public class Example002 {
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner();
        inner .test2();
        outer.show2();
    }

}

局部内部类

又叫方法内部类,此类可以访问外部类,但是不能让外部类访问自己。

class Outer{
    int m = 0;
    public void test1(){
        System.out.println("外部成员类方法");
    }
    void test2(){
        class Inner{

            int n=1;
            void show(){
                System.out.println("外部类变量m="+m);
            test1();
            }
        }
        Inner inner = new Inner();
        System.out.println("局部内部类变量n=" + inner.n);
        inner.show();
    }

    }
public class Example002 {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.test2();
    }

}

静态内部类

使用static关键字修饰,并且只能内部类访问外部类,外界访问内部类时,可以不通过本内部类的外部类,直接进行访问。

class Outer{
    static int m = 0;
   static class Inner{
       void show(){
           System.out.println("外部静态类m="+m);
       }
   }

    }
public class Example002 {
    public static void main(String[] args) {
        Outer.Inner inner = new Outer.Inner();
        inner.show();
    }

}

匿名内部类

本了类就是没有被实名化的来创立对象,当需要传一个参数接口实现类的时候,我们一般不再去创建一个新的对象来实现这个接口,直接实现匿名内部类。

interface Animal{
     void shout();
}


public class Example001 {
    public static void main(String[] args) {
        String name = "小龙";
        animalshout(new Animal() {
            public void shout() {
                System.out.println(name+"喵喵喵");
            }
        });
    }
        public static void animalshout(Animal an){
            an.shout();
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值