• JAVA abstract 抽象类总结


    1、在类中,如果存在方法名与类名相同,此时该方法前可以不用加void,也可以省略public。除此之外的任何方法名,都必须有修饰。否则报错。

    public abstract class Animal {
    	Animal() {
    		System.out.println("Animal1111");
        }
    	public void Animalb()
    	{
    		System.out.println("Animalbbbbbb");
    	}
    	public abstract void Animalc();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2、继承类中,同样的道理,如果存在方法名与类名相同,此时该方法前可以不用加void,也可以省略public

    
    public class Bird extends Animal {
    
    	@Override
    	public void Animalc() {
    		// TODO Auto-generated method stub
    		System.out.println("Animalccccccc");
    	}
    
    	Bird()
    	{
    		super();
    	}
    
    	public static void main(String[] args)
    	{
    		Bird bird =new Bird();
    		bird.Animalb();
    		bird.Animalc();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3、 Bird 方法中,默认存在super()方法,即自动调用Animal 中的同名方法。也就是说,即便Bird 方法中,什么都没写,依然会输出:

    System.out.println("Animal1111");
    
    • 1
    public abstract class Animal {
    	Animal() {
    		System.out.println("Animal1111");
        }
    	public void Animalb()
    	{
    		System.out.println("Animalbbbbbb");
    	}
    	public abstract void Animalc();
    }
    
    public class Bird extends Animal {
    
    	@Override
    	public void Animalc() {
    		// TODO Auto-generated method stub
    		System.out.println("Animalccccccc");
    	}
    
    	Bird()
    	{
    
    	}
    
    	public static void main(String[] args)
    	{
    		Bird bird =new Bird();
    		bird.Animalb();
    		bird.Animalc();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    上面 Bird()是空函数,但Bird bird =new Bird() 实例化的第一时间,Bird就自动去继承Animal类的Animal方法了。
    也就相当于写了super()

    4、 什么时候默认super()报错呢?

    当父类的同名方法中,包含参数时,此时子类就需要手动添加带参数的super了。

    
    public abstract class Animal {
    	
    	Animal(int a) {
    		System.out.println("Animal1111");
        }
        
    	public void Animalb()
    	{
    		System.out.println("Animalbbbbbb");
    	}
    	public abstract void Animalc();
    }
    
    
    
    public class Bird extends Animal {
    
    	@Override
    	public void Animalc() {
    		// TODO Auto-generated method stub
    		System.out.println("Animalccccccc");
    	}
    
    	 Bird()
    	{
    		super(5);
    		
    	}
    	
    	public static void main(String[] args)
    	{	
    		Bird bird =new Bird();
    		bird.Animalb();
    		bird.Animalc();
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    由于Animal类中的Animal方法包含了参数,此时如果在Bird类的Bird方法中不写super,也就等着系统默认添加:

    super()
    
    • 1

    此时必然报错。因为系统添加的super()是一个无参的,与实际父类定义不同。所以需手动更改成带参数的:

    super(5)
    
    • 1

    父类中同名方法,不能被调用。因为没有public void 等的修饰。

    
    public class Bird extends Animal {
    
    	@Override
    	public void Animalc() {
    		// TODO Auto-generated method stub
    		System.out.println("Animalccccccc");
    	}
    
    	 Bird()
    	{
    		super(5);
    	}
    	
    	public static void main(String[] args)
    	{	
    		Bird bird =new Bird();
    		bird.Animalb();
    		bird.Animalc();
    		
    		bird.Animal();							 //这句报错。父类中的Animal方法不能被调用。
    		
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    super()方法,只能放在子类的同名方法中,放其他方法报错。

    
    public class Bird extends Animal {
    
    	@Override
    	public void Animalc() {
    		// TODO Auto-generated method stub
    		System.out.println("Animalccccccc");
    	}
    
    	 Bird()
    	{
    		super(5);	
    	}
    	
    	public void Bird1()
    	{
    		super(5);							//这句报错。
    		System.out.println("bird11111");
    	}
    	
    	public static void main(String[] args)
    	{
    		Bird bird =new Bird();
    		bird.Animalb();
    		bird.Animalc();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    抽象方法

    参考
    抽象方法表示没有实现的方法,没有方法体的方法。例如:

    public abstract void doSome();  //注意:没有“{ }”。
    
    • 1

    抽象方法特点是:前面使用abstract修饰,没有方法体,以分号结尾。

    非抽象类继承抽象类,子类一定,必须得覆盖父类中的抽象方法。

    
    public abstract class Animal {
    	
    	Animal(int a) {
    		System.out.println("Animal1111");
        }
        
    	public void Animalb()
    	{
    		System.out.println("Animalbbbbbb");
    	}
    	public abstract void Animalc();
    }
    
    
    
    public class Bird extends Animal {
    
    1	@Override
    2	public void Animalc() {
    3		// TODO Auto-generated method stub
    4		System.out.println("Animalccccccc");
    5	}
    
    	 Bird()
    	{
    		super(5);
    		
    	}
    	
    	public static void main(String[] args)
    	{	
    		Bird bird =new Bird();
    		bird.Animalb();
    		bird.Animalc();
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    1~5行,是系统默认添加的,是父类中抽象方法。

    override

    参考

    编译器可以给你验证@Override下面的方法名称是否是你父类中所有的,如果没有就会报错。

    比如当你想要在子类中重写父类的一个方法,但是你把名字打错了,当你写了@Override编译器会提示你,你写的这个方法父类中没有;但是如果你没有写@Override编译器就会觉得这个是你子类中写的新的方法,并不会报错,到时候你debug还是很麻烦的一件事。

  • 相关阅读:
    Stable Diffusion 参数介绍及用法
    北京化工大学数据结构2022/10/27作业 题解
    AI面试必备-《家居必备的AI精选资源列表》免费分享
    400电话和95开头的电话有什么区别吗?
    【C++】C++入门(中)--引用
    MySQL-8.0 事务隔离级别
    VUE3脚手架工具cli配置搭建及创建VUE工程
    在PyCharm中直接启动mitmproxy并自动打开&关闭系统代理
    如何处理linux 自动执行的sh脚本
    最详细MySql安装教程
  • 原文地址:https://blog.csdn.net/qq_41749451/article/details/126362174