抽象类和接口基础知识总结及代码

多态

向上转型,动态绑定

class Shape{
    public void draw(){
        System.out.println("Shape::draw()");
    }
}

class Cycle extends Shape{
    @Override
    public void draw() {
        System.out.println("画圆");
    }
}

class Rect extends Shape{
    @Override
    public void draw() {
        //super.draw();
        System.out.println("画矩形");
    }
}

代码块的执行顺序测试

class Animal{
    protected String name;
    protected int age;

    static{
        System.out.println("Animal::static{}");
    }

    {
        System.out.println("Animal::instance{}");
    }

    public Animal(String name,int age){
        this.name = name;
        this.age = age;
        System.out.println("Animal(String name,int age)");
    }


    void eat(){
        System.out.println("Animal::eat()");
    }

}

class Cat extends Animal{
    public String sex;

    public Cat(String name, int age,String sex) {
        super(name, age);
        this.sex = sex;
        System.out.println("Cat(String name, int age,String sex)");
    }

    public void show(){
        System.out.println(this.name + " " + this.age);
    }
   // @Override
    public void eat(){
        System.out.println(this.name + "Cat::eat() ");
    }
}

public class TestDemo2 {
    public static void func1(){
        System.out.println("func1()::static{}");
    }
    public static void main(String[] args) {
        Animal animal = new Cat("琼琼", 18, "girl");
        animal.eat();
        func1();
    }
}

抽象类

/*
* 0、抽象方法:没有实现的方法   abstract
* 1、抽象类是包含抽象方法的类   abstract
* 2、抽象类可以包含抽象方法,也可以包含非抽象方法
* 3、抽象类不可以被实例化,不能  Shape1 shape1 = new Shape1();
* 4、一个普通的类,如果继承了抽象类,那么一定要重写抽象类的抽象方法
* 5、抽象类A可以继承抽象类B,这样一来就不需要重写抽象方法
* 6、如果一个普通类C继承了上述的抽象类A,如果上述抽象类A并没有重写抽象类B的抽象方法,那么当前的
* 普通类C一定要重写A和B的方法;
* 7、抽象方法一定不能是private   抽象类不能是final所修饰
*/
abstract class Shape1{
    public abstract void draw();
}

abstract class A extends Shape1{

}

class B extends A{
    @Override
    public void draw() {

    }
}

class Cycle1 extends Shape1{
    @Override
    public void draw() {
        System.out.println("画圆");
    }
}

class Rect1 extends Shape1{
    @Override
    public void draw() {
        System.out.println("画矩形");
    }
}

public class TestDemo3 {
    public static void draw(Shape1 shape1){
        shape1.draw();
    }

    public static void main(String[] args) {
        Shape1 shape1 = new Cycle1();
        Shape1 shape2 = new Rect1();
        draw(shape1);
        draw(shape2);
    }
}

接口

测试1

/*
* 0、接口当中的方法一定不能有具体的实现  抽象方法    interface
* 2、void draw()
*          默认public abstract void draw();
* 3、int a = 10;
*          默认public static final int a = 10;
* 4、普通的类不能继承接口,但是可以实现接口    implements
* 5、接口是不可以实例化的,但是可以发生向上转型
* 6、一个类可以实现多个接口
*  class Dog extends Animal2 implements IRunning,TSwimming;
* 7、抽象类是可以实现接口的
* 8、接口和接口之间是不能发生实现的。
* 但他们之间可以继承,interface C extends B,A    A和B均为接口
*/
interface IShape{
    void draw();
}

class Cycle2 implements IShape{
    @Override
    public void draw() {
        System.out.println("画圆");
    }
}

class Rect2 implements IShape{
    @Override
    public void draw() {
        System.out.println("画矩形");
    }
}

public class TestDemo4 {
    public static void main(String[] args) {
        IShape shape = new Cycle2();
        IShape shape1 = new Rect2();

        shape.draw();
        shape1.draw();
    }

}

测试2

interface IFlying{
    void run();
}

interface IRunning{
    void run();
}

interface ISwimming{
    public abstract void swim();
}

class Animal2{
    protected String name;
    public Animal2(String name){
        this.name = name;
    }
}

class Cat2 extends Animal2 implements IRunning{
    public Cat2(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println("猫跑");
    }
}
class Dog extends Animal2 implements IRunning,ISwimming{
    public Dog(String name) {
        super(name);
    }

    @Override
    public void swim() {
        System.out.println("狗游泳");
    }

    @Override
    public void run() {
        System.out.println("狗跑");
    }

}
abstract class AA implements IRunning{
//    @Override
//    public void run() {
//
//    }
}
class BB extends AA{
    @Override
    public void run() {

    }
}

public class TestDemo5 {

    public static void fun1(IRunning iRunning){
        iRunning.run();
    }

    public static void main(String[] args) {
        Cat2 cat = new Cat2("q琼琼");
        fun1(cat);
        Dog dog = new Dog("琼宝");
        fun1(dog);
    }
}

测试3

interface A1{
    void func0();
}

interface B1{
    void func1();
}

interface C1 extends A1,B1{
    void func2();
}

class D implements C1{
    @Override
    public void func2() {

    }

    @Override
    public void func1() {

    }

    @Override
    public void func0() {

    }
}


public class TestDemo6 {
    public static void main(String[] args) {

    }
}

排序打印类的信息

import java.util.Arrays;


class Student implements Comparable<Student>{
    private String name;
    private int age;
    private double score;

    public Student(String name, int age, double score){
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
    @Override
    public int compareTo(Student o) {
        //return this.age - age;
        //return this.name.compareTo(0.name);
        return (int)(this.score - o.score);
    }
}



public class TestDemo7 {

    public static void main(String[] args) {
        Student[] students = new Student[2];
        students[0] = new Student("baby",16,66);
        students[1] = new Student("honey",18,99);
        System.out.println(Arrays.toString(students));
        sort(students);
        System.out.println(Arrays.toString(students));
    }


    public static void main1(String[] args) {
        int[] array = {10,4,1,9,8};
        System.out.println(Arrays.toString(array));
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void sort(Comparable[] array) {
        for (int bound = 0; bound < array.length; bound++) {
            for (int cur = array.length - 1; cur > bound; cur--) {
                if (array[cur - 1].compareTo(array[cur]) > 0) {
// 说明顺序不符合要求, 交换两个变量的位置
                    Comparable tmp = array[cur - 1];
                    array[cur - 1] = array[cur];
                    array[cur] = tmp;
                }
            }
        }
    }

}

类的clone();

//通过person2改变money
/*
* 自定义的类型 如果进行克隆
* 1、implements Cloneable   它是一个空接口
*  面试问题;为什么Cloneable是一个空接口?
*              标记接口; 标记当前类可以进行clone();
* 2、重写一个方法:是   object::clone()
* 3、处理异常
*/
class Money implements  Cloneable{
    double money = 50.0;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable{
    public String name;
    public int age;

    Money m;

    public Person(){

    }
    public Person(String name, int age){
        this.name = name;
        this.age = age;
        this.m = new Money();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", m=" + m +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person p = (Person)super.clone();

        p.m = (Money) this.m.clone();
        return p;
        //return super.clone();
    }
}


public class TestDemo8 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person("baby",18);
        Person person1 = (Person) person.clone();

        System.out.println(person.m.money);
        System.out.println(person1.m.money);
        System.out.println(person);
        System.out.println(person1);

        System.out.println("通过person2改变money");
        person1.m.money = 100.0;

        System.out.println(person.m.money);
        System.out.println(person1.m.money);
        System.out.println(person);
        System.out.println(person1);
    }
}
发布了28 篇原创文章 · 获赞 3 · 访问量 743

猜你喜欢

转载自blog.csdn.net/XDtobaby/article/details/102828547
今日推荐