今天在老师的带领下学习了抽象类(abstract)和接口(interface),总结一下今天所学的知识。

抽象类是在学习到接口时之前讲解,作为学习接口的铺垫。

 

下面是我今天写的例题。

一个Shape(形状)类抽象类为父类。

两个子类,Triangle(三角形) Circle(圆)类;

一个坐标类Point,用来帮助求面积和周长。

父类形状类--》 Shape:


public abstract class Shape {
public String name;

public String getName() {
return name;
}

/**
* 抽象类,求周长
* @return 周长的值
*/
public abstract double perimeter();

/**
* 抽象类,求面积
* @return 面积
*/
public abstract double area();
}


Point类(辅助类,帮助求三角形和圆的面积周长)


public class Point {
private double x; //成员变量
private double y;

/**
* 初始化方法
* @param x 横坐标
* @param y 纵坐标
*/
public void init(double x,double y){
this.x = x;
this.y = y;
}
/**
*
* @param p 计算距离的两个点之一(还有一个是this,即自己)
* @return 距离
*/
public double distance(Point p){
return Math.sqrt(Math.pow((p.x-this.x),2)+Math.pow((p.y-this.y),2)); // 当前对象 p1.distance(p2)) this 表示的是 p1
}
}

三角形类(Triangle)


public class Triangle extends Shape {
private Point ponitA ;// 坐标A
private Point ponitB ;// 坐标B
private Point ponitC ;// 坐标C

@Override
public String getName() {
return "三角形";
}


public Triangle(double x1,double y1,double x2,double y2,double x3,double y3){
ponitA = new Point();
ponitA.init(x1,y1);
ponitB = new Point();
ponitB.init(x2,y2);
ponitC = new Point();
ponitC.init(x3,y3);

}

/**
* 初始化三角形
* @param ponitA 第一个点
* @param ponitB 第二个点
* @param ponitC 第三个点
*/
public Triangle(Point ponitA,Point ponitB,Point ponitC){
this.ponitA = ponitA;
this.ponitB = ponitB;
this.ponitC = ponitC;
}

/**
* 计算周长
* @return 周长
*/
public double perimeter(){

double a = ponitA.distance(ponitB); //a到b的距离
double b = ponitB.distance(ponitC); //b到c的距离
double c = ponitC.distance(ponitA); //c到a的距离

return a+b+c;
}

/**
* 计算面积
* @return 面积
*/
public double area(){
new Point();
double a = ponitA.distance(ponitB); //a到b的距离
double b = ponitB.distance(ponitC); //b到c的距离
double c = ponitC.distance(ponitA); //c到a的距离

double s = (a+b+c)/2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
}

圆类 Circle


public class Circle extends Shape{
private double r;
private Point p;// 圆心

@Override
public String getName() {
return "圆";
}
/**
* 初始化
* @param r 初始化半径
* @param x 初始化x
* @param y 初始化y
*/
public Circle(double r, double x,double y){
this.r = r;
Point p = new Point();
p.init(x,y);
this.p = p;
}

/**
* 第二种初始化方法
* @param p 定义一个坐标
* @param r 初始化半径
*/
public Circle(Point p,double r){
new Point();
this.p = p;
this.r = r;
}

/**
* 求面积
* @return 返回面积
*/
public double area( ){
return Math.PI*r*r;
}

/**
* 求周长
* @return 返回周长
*/
public double perimeter( ){
return Math.PI*r*2;
}
}

测试类 Test


public class Test {
public static void main(String[] args) {
Shape shape ;
Circle circle = new Circle(1,0,0);

Triangle triangle = new Triangle(0,0,0,3,4,0);
print(circle);

shape = triangle;//赋值兼容性规则,凡是需要用到父类对象的时候,都可以用子类代替它。
print(shape);

}
public static void print(Shape shape){
System.out.println(shape.getName()+"的周长是:"+shape.perimeter());
System.out.println(shape.getName()+"的面积是:"+shape.area());
}
}
 
测试结果:



学习总结

.抽象类-- 抽象类不可以用于对象创建, 比如说 Shape shape = new Shape();是错误的,只能 ---》Shape shape;


Shape shape = new Shape() 的时候会弹出来如下图所示,这个叫做匿名内部类



这个匿名类简化了代码,把偶尔用到的类进行内部匿名。它实现了把接口,定义对象合二为一,是目前的主流写法。


要注意的是:匿名类在 代码段合起来以后 就像是一个语句,后面要加分号,这里容易出错。


 


匿名类应用最多的地方是键盘事件,下图是做贪吃蛇时用到的匿名类