栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java面向对象

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java面向对象

多态

多态:同一种事物在不同时刻表现出来不同状态条件:1.继承2.重写3.父类引用指向子类对象

示例

public abstract  class Animal {
    
    int a=10;
    int b=8;
    public abstract void eat();
    public static void show(){
        System.out.println("动物");
    }
}
​
public class Cat extends Animal{
​
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
​
    public static void show(){
        System.out.println("猫");
    }
}
​
​
public class Dog extends Animal{
​
    @Override
    public void eat() {
        System.out.println("狗吃骨头");
    }
​
    public void play(){
        System.out.println("狗会玩");
    }
}
​
​
public class Rabbit extends Animal{
    int a= 5;
    @Override
    public void eat() {
        System.out.println("兔子吃草");
    }
    public static void show(){
        System.out.println("兔子");
    }
}
​
​
public class Test {
    public static void main(String[] args) {
        Animal cat=new Cat();
        cat.eat();//猫吃鱼    编译看左边,运行看右边(编译时期类型是父类,运行时期类型是子类)
        cat.show();//动物     静态方法编译运行都看左边
        Animal rabbit=new Rabbit();
        rabbit.eat();//兔子吃草
        System.out.println(rabbit.a);//10  成员变量编译运行都看左边
        System.out.println(rabbit.b);//8
    }
}
​
​
public class Test1 {
    public static void main(String[] args) {
        Test1 test=new Test1();
        Animal cat=new Cat();
        test.feedAnimal(cat);
        Animal rabbit=new Rabbit();
        test.feedAnimal(rabbit);
        Dog dog=new Dog();
        test.feedAnimal(dog);
    }
    public void feedAnimal(Animal animal){
        animal.eat();
        Dog d=(Dog) animal;//向下转型
        if(animal instanceof Dog){
            d.play();
        }
    }
}

final关键字

final关键字

可以修饰属性,方法,类 修饰属性

修饰的成员变量为常量,值不可以再修改

修饰方法 子类不可以重写被final修饰的方法

修饰类 不可以再被继承

public class Demo1 {
    
    public final static int NUM=0;//用final修饰的成员变量常用static修饰
    public final  int num1;//定义没有赋值时,应在构造方法中对其赋值
    public Demo1(){
        num1=0;
    }
    public Demo1(int num1){
        this.num1=num1;
    }
    public final void show(){
        System.out.println("示例");
    }
    public void show1(){
        System.out.println("示例1");
    }
}
​
public class Demo2 extends Demo1{
    //public void show(){}不可以重写被final修饰的
    public void show1(){
        System.out.println("重写");
    }
}
接口
示例

public interface MyInterface extends MyInterface1,MyInterface2,MyInterface3{
    
    //定义常量
    public static  final int NUM=10;
    //定义抽象方法
    public abstract void show();
    public abstract void sleep();
    //定义静态方法
    public static  void showinfo(){
        System.out.println("这是静态方法");
    }
    //定义默认方法
    public default void showinfo1(){
        System.out.println("这是默认方法");
    }//由子类使用
}
​
​
public interface MyInterface1 {
    void play();
}
​
​
public interface MyInterface2 {
    void sing();
}
​
​
public interface MyInterface3 {
    void run();
}
​
public class MyInterface1Impl implements MyInterface3,MyInterface,MyInterface2,MyInterface1{
    @Override
    public void show() {
    }
​
    @Override
    public void sleep() {
​
    }
​
    @Override
    public void play() {
​
    }
​
    @Override
    public void sing() {
​
    }
​
    @Override
    public void run() {
​
    }
​
    public static void main(String[] args) {
        MyInterface.showinfo();
    }
}
​
​
//public abstract  class MyInterfaceImpl implements MyInterface{
public class MyInterfaceImpl implements MyInterface{
    @Override
    public void show() {
​
    }
​
    @Override
    public void sleep() {
​
    }
​
    @Override
    public void play() {
​
    }
​
    @Override
    public void sing() {
​
    }
​
    @Override
    public void run() {
​
    }
}

接口与抽象类的异同

public abstract class Animal {
    private String color;
    private int age;
​
    public Animal() {
    }
​
    public Animal(String color, int age) {
        this.color = color;
        this.age = age;
    }
​
    public abstract void eat();
​
    public String getColor() {
        return color;
    }
​
    public void setColor(String color) {
        this.color = color;
    }
​
    public int getAge() {
        return age;
    }
​
    public void setAge(int age) {
        this.age = age;
    }
}
​
​
public class Dog extends Animal implements Play{
    @Override
    public void eat() {
        System.out.println("狗吃骨头");
    }
​
    @Override
    public void play() {
        System.out.println("狗会玩");
    }
}
​
​
public class Rabbit extends Animal implements Jump,Play {
    @Override
    public void eat() {
        System.out.println("兔子吃草");
    }
​
    @Override
    public void jump() {
        System.out.println("兔子会跳");
    }
​
    @Override
    public void play() {
        System.out.println("兔子会玩");
    }
}
​
public class Fish extends Animal implements  Swim{
    @Override
    public void eat() {
        System.out.println("鱼吃虾米");
    }
​
    @Override
    public void swim() {
        System.out.println("鱼会游");
    }
}
​
​
public interface Play {
    void play();
}
​
public interface Jump {
    void jump();
}
​
public interface Swim {
    void swim();
}
​
public class Test {
    public static void main(String[] args) {
        
        Animal dog=new Dog();
        dog.eat();
        Animal rabbit= new Rabbit();
        rabbit.eat();
        Animal fish =new Fish();
        fish.eat();
        Play p=new Dog();
        p.play();
        Jump j=new Rabbit();
        j.jump();
        Swim s=new Fish();
        s.swim();
    }
}

注:全部代码在一个包中,分为不同的类

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/970506.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号