Java abstract修饰符

1.abstract修饰类:抽象类

 1)不可被实例化

 2)抽象类有构造器(凡是类都有构造器)

 3)抽象方法所在的类,一定是抽象类

 4)抽象类中,可以没有抽象方法


2.abstract修饰方法:抽象方法

 1)格式:没有方法体且没有{}。如:public abstract void eat();

 2)抽象方法只保留方法的功能,而具体的执行交给继承抽象类的子类,由子类重写此抽象方法

 3)若子类继承抽象类,并重写了所有的抽象方法,则此类可以实例化

 4)若子类继承抽象类,没有重写所有的抽象方法,以为这此类中仍有抽象方法,则此类必须声明为abstract,即抽象类


public class TestAbstract {
	public static void main(String[] args) {
		Person person0 = new Student();
		person0.eat();
		System.out.println();
		Person person1 = new Student("小明");
		person1.walk();
		
		System.out.println();
		
		Person person2 = new Worker();
		person2.say();//调用抽象方法中的已实现的方法
		System.out.println();
		Person person3 = new Worker("小东");
		person3.eat();
	}
}

abstract class Creator {
	public abstract void breath();
}

//抽象类继承抽象类
abstract class Person extends Creator {
	String name;
	
	//抽象类有构造器,子类会调用,但是抽象类本身不能被实例化
	public Person() {
		System.out.println("into Person() constructor");
	}
	
	public Person(String name) {
		this.name = name;
		System.out.println("into Person(String name) constructor");
	}
	
	public abstract void eat();
	
	public abstract void walk();
	
	//抽象类中可以有非抽象的方法
	public void say() {
		System.out.println("human saying...");
	}
	
	//实现抽象的父类的breath()方法
	public void breath() {
		System.out.println("human breath...");
	}
}

class Student extends Person {
	public Student() {
		super();
		System.out.println("into Student() constructor");
	}
	
	public Student(String name) {
		super(name);
		System.out.println("into Student(String name) contructor");
	}

	@Override
	public void eat() {
		System.out.println("Student eating...");
	}

	@Override
	public void walk() {
		System.out.println("Student walking...");
	}
}

class Worker extends Person {
	public Worker() {
		super();
		System.out.println("into Worker() constructor");
	}
	
	public Worker(String name) {
		super(name);
		System.out.println("into Worker(String name) contructor");
	}

	@Override
	public void eat() {
		System.out.println("Worker eating...");
	}

	@Override
	public void walk() {
		System.out.println("Worker walking...");
	}
}

结果:

into Person() constructor
into Student() constructor
Student eating...

into Person(String name) constructor
into Student(String name) contructor
Student walking...

into Person() constructor
into Worker() constructor
human saying...

into Person(String name) constructor
into Worker(String name) contructor
Worker eating...

注意事项:

       abstract不能用来修饰属性、构造器、private、final、static


错误案例:

public class ErrorAbstract {
	
}

abstract class A {
	//一个类中的属性只属于这个类,子类没机会重写
	abstract int num;//出错
	
	//因为构造器不能被重写,所以不能用abstract
	public abstract A();//出错
	
	//子类不能覆盖(或重写)声明为private的父类方法
	private abstract void method1();//出错
	
	//abstract希望被子类重写,然而final禁止子类重写
	public abstract final void method2();//出错
	
	//static修饰的类方法可以被类直接调用,即A.method3(),
	//但是abstract修饰的类并没用执行的具体方法
	public abstract static void method3();//出错
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值