java基础部分的简单应用

牛刀小试,MMP;嘿嘿,如有转载,请声明地址http://www.cnblogs.com/jinmoon/;

图形类,点类,三角形类,汽车类,接口;运用继承,抽象类,接口,多态;已知点类三点,输出三点;三角形类周长,面积;汽车标志;

public class Test {
public static void main(String[] args) {
SS p,w,e,r,t;
Point P1=new Point("hong",1, 2);
Point P2=new Point("huang",15, 4);
Point P3=new Point("nan",21, 12);
San san=new San("hei", P1, P2, P3);
Car car=new Car("baoma");
p=P1;w=P2;e=P3;r=san;t=car;
Test kk=new Test();
kk.showAnny(p);
kk.showAnny(w);
kk.showAnny(e);
kk.showAnny(r);
kk.showAnny(t);
}
public void showAnny(SS p)
{
p.show2();
}

}
class Point extends Shape implements SS //点类
{
double x,y;
public Point( double x,double y)
{
this.x=x;
this.y=y;
}

public Point(String color, double x,double y)
{
super(color);
this.x=x;
this.y=y;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public String show()
{
return "("+x+","+y+")";
}
public double zhouChang()
{
return 0;
}
public void show2()
{
System.out.print( super.toString()+" "+"Point;"+"("+x+","+y+")"+" 周长是:"+zhouChang());
}
}

class San extends Shape implements SS //三角形类
{
double q,s,h;
double a,b,c;
Point P1;
Point P2;
Point P3;
public San(String color,Point p1,Point p2,Point p3)
{
super(color);
this.P1=p1;
this.P2=p2;
this.P3=p3;
}
public double getA()
{
a=Math.round(Math.sqrt((P1.getX()-P2.getX())*(P1.getX()-P2.getX())+(P1.getY()-P2.getY())*(P1.getY()-P2.getY())));
return a;
}
public double getB()
{
b=Math.round(Math.sqrt((P1.x-P3.x)*(P1.x-P3.x)+(P1.y-P3.y)*(P1.y-P3.y)));
return b;
}
public double getC()
{
c=Math.round(Math.sqrt((P3.x-P2.x)*(P3.x-P2.x)+(P3.y-P2.y)*(P3.y-P2.y)));
return c;
}
public double getS()
{

q=(a+b+c)/2;
s=Math.round(Math.sqrt(q*(q-a)*(q-b)*(q-c)));
return s;
}
public double mianJi()
{
getA();
getB();
getC();
if(a+b>c&&a-b<c)
{
s=getS();
return s;
}
else
{
return 0;
}
}
public double zhouChang()
{
h=getA()+getB()+getC();
return h;
}
public void show2()
{
System.out.print( super.toString()+" A=:"+P1.show()+" B:="+P2.show()+" C;="+P3.show()+" 周长是:"+zhouChang()+" 面积是:"+mianJi());
}

}
abstract class Shape //抽象的图形类
{
private String color;
public Shape(){}
public Shape(String color)
{
this.color=color;
}
public abstract double zhouChang();
public String toString()
{
return " Color:"+color;
}
}
interface SS //接口,抽象的一个输出方法
{
void show2();
}
class Car implements SS //汽车类
{
String logal;
public Car(String logal)
{
this.logal=logal;
}
public void show2()
{
System.out.print(" logal是 "+logal);
}
}

原文地址:https://www.cnblogs.com/jinmoon/p/8000866.html