您当前的位置: 首页 >  Java

星夜孤帆

暂无认证

  • 4浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java学习笔记(19)-抽象类

星夜孤帆 发布时间:2018-05-22 09:31:04 ,浏览量:4

抽象类

    抽象方法:只有方法名和参数列表,没有方法体。

    用于描述抽象的功能,多用于功能设计。定义软件之间的约定功能。

    Timer类约定:在适合时候调用TimerTask的抽象方法run()

    使用Timer只需要实现run方法就可以实现定时任务。

    Timer定时的“回调”run方法实现定时任务。

一般把包含抽象方法的类定义为抽象类

抽象类可以定义变量,不能直接实例化。

抽象类只能被继承

具体类继承抽象类时候必须“实现”(重写)全部抽象方法。

package day04;
/*
 * 抽象类,用于表示抽象概念,这里表示抽象的图形概念
 * 包含抽象方法的类一定是抽象类
 * 抽象类可以定义变量,抽象类不能直接实例化,抽象只能被继承
 * 具体类在继承抽象类时候必须实现全部的抽象方法
 */
public abstract class Shape {
	protected int x;
	protected int y;
	/*具体方法,有方法体*/
	public void up(){
		y--;
	}
	/*抽象方法,没有方法体*/
	public abstract boolean contains(int x,int y);

}
package day04;

public class Circle extends Shape {
	private int r;
	public Circle(){
	}
	public Circle(int x,int y,int r){
		this.r = r;
		this.x = x;//this.x继承于Shape类
		this.y = y;
	}
	/* 实现了抽象类Shape中的抽象方法contains*/
	public boolean contains(int x,int y){
		int a = this.x -x;
		int b = this.y -y;
		double c = Math.sqrt(a*a+b*b);
		return c            
关注
打赏
1636984416
查看更多评论
0.0379s