Java SE 021 抽象类

(1)一个人只要自己不放弃自己,整个世界也不会放弃你.
(2)天生我才必有大用
(3)不能忍受学习之苦就一定要忍受生活之苦,这是多么痛苦而深刻的领悟.
(4)做难事必有所得
(5)精神乃真正的刀锋
(6)战胜对手有两次,第一次在内心中.
(7)编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~

Java SE 021 抽象类

1.抽象类(abstract class)

(1)使用了abstract关键字所修饰的类叫抽象类。

(2)抽象类无法实例化,也就是说,不能new出来一个抽象的对象(实例)。

2.抽象方法(abstract method)

(1)使用abstract所修饰的方法叫做抽象方法。

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

abstract class T{
	//有声明无实现
	public abstract void method();
}

(2)有{}与没{}是两个完全不同的概念。有{}表示有方法实现,没有{}则表是无方法实现。

3. 抽象类与抽象方法之间的关系

1、抽象方法需要定义在抽象类当中。相对于抽象方法,之前所定义的方法叫做具体方法(有声明有实现)。

2、如果一个类包含了抽象方法,那么这个类一定是抽象类。

3、如果某个类是抽象类,那么该类可以包含具体方法(有声明有实现)。

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

abstract class T{
	//有声明无实现
	public abstract void method();

	//具体方法
	public void test(){
		System.out.println("test");
	}
}

4、如果一个类中包含了抽象方法,那么这个类一定要声明成abstract class,也就是说,该类一定是抽象类;反之,如果一个类是抽象类,那么该类即可以包含抽象类,也可以包含具体方法。

5、无论何种情况,只要一个类是抽象类,那么这个类就无法实例化。

4.在子类继承父类(父类是个抽象类)的情况下:

(1)子类必须要实现父类中所定义的所有抽象方法,否则该子类要声明成一个abstract class。

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

abstract class T{
	//有声明无实现
	public abstract void method();

	//具体方法
	public void test(){
		System.out.println("test");
	}
}

class R extends T{
	public void method(){
		System.out.println("method");
	}
}

5.抽象类到底起个什么样的作用呢?

(1)一个方法光有声明,没有实现,有什么价值呢?

光有声明没有实现,肯定是推迟到子类当中去实现的。父类肯定实现不了了。我要实现这个方法,肯定要推迟到子类里面去实现。

(2)原则:在父类里面仅仅是定义了一个方法声明,这个方法声明可以写些注释,告诉这个类的实现者,这个方法到底要干嘛用的,干什么用的,但具体怎么去做这件事情,是由具体的子类实现者他来提供的。那么对于一个父类来说,它可能有多个子类,不同的子类可能对于这个父类来说,有不同的实现方式。

public class Test2{
	public static void main(String[] args){
		Shape shape = new Triangle(10,6);
		int area = shape.computerArea();
		System.out.println("triangle area is: "+area);

		shape = new Rectangle(8,6);
		area = shape.computerArea();
		System.out.println("Rectangle area is: "+area);
	}
}
//在抽象类里面定义了一个方法,它只有一个抽象方法,计算形状的面积,但是具体什么形状,其实是由子类本身来决定的。公式也是由子类本身来去决定的。
abstract class Shape{
	public abstract int computerArea();//计算形状面积
}

class Triangle extends Shape{
	int width;
	int height;
	public Triangle(int width,int height){
		this.width = width;
		this.height = height;
	}
	public int computerArea(){
		return (this.width*this.height)/2;
	}
}

class Rectangle extends Shape{
	int width;
	int height;
	public Rectangle (int width,int height){
		this.width = width;
		this.height = height;
	}
	public int computerArea(){
		return this.width*this.height;
	}
}

(1)抽象方法只是起到一个约束或者说一种规范。

上例中,抽象方法计算机圆的面积,到底怎么计算,怎么实现,是由子类自己来去决定的。它自己不负责实现,只是把这个规则给表示出来。

(2)方法声明就是一种规则。

  • 方法该接收什么参数,我这方法要返回什么值,这是方法的一种规则。

  • 那么具体怎么去操纵这种规则,怎么去实现,满足这个规则的要求,是由子类自己来去决定的,来去实现的。

  • 父类(抽象类)不管这些事情。它没法去管,因为对这个程序来说,它管不管根本没有什么意义。

(3)不把它定义成抽象方法,不能说是不可以的,但是这样是不好的。

  • 如下代码也是没问题的,结果也是正确的,但这么做显然不如刚才定义抽象方法那么好,但是别人一看这程序,return 0;是什么意思呢?
  • 这个方法实现没有任何意义。
  • 换句话说,我返回1,2,3…都没有任何意义。
  • 一个没有意义的结果有什么意义呢?
  • 所以这个地方最好是将它定义成抽象的。让它去规范子类的一个行为。
public class Test2{
	public static void main(String[] args){
		Shape shape = new Triangle(10,6);
		int area = shape.computerArea();
		System.out.println("triangle area is: "+area);

		shape = new Rectangle(8,6);
		area = shape.computerArea();
		System.out.println("Rectangle area is: "+area);
	}
}

class Shape{
	//计算形状面积
	public  int computerArea(){
	    return 0;
	}
}

class Triangle extends Shape{
	int width;
	int height;
	public Triangle(int width,int height){
		this.width = width;
		this.height = height;
	}
	public int computerArea(){
		return (this.width*this.height)/2;
	}
}

class Rectangle extends Shape{
	int width;
	int height;
	public Rectangle(int width,int height){
		this.width = width;
		this.height = height;
	}
	public int computerArea(){
		return this.width*this.height;
	}
}
extends Shape{
	int width;
	int height;
	public Rectangle(int width,int height){
		this.width = width;
		this.height = height;
	}
	public int computerArea(){
		return this.width*this.height;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值