java的简单应用_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+")"+"\n周长是:"+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

{

s=getS();

return s;

}

else

{

return 0;

}

}

public double zhouChang()

{

h=getA()+getB()+getC();

return h;

}

public void show2()

{

System.out.print( super.toString()+"\nA=:"+P1.show()+"\nB:="+P2.show()+"\nC;="+P3.show()+"\n周长是:"+zhouChang()+"\n面积是:"+mianJi());

}

}

abstract class Shape //抽象的图形类

{

private String color;

public Shape(){}

public Shape(String color)

{

this.color=color;

}

public abstract double zhouChang();

public String toString()

{

return "\nColor:"+color;

}

}

interface SS //接口,抽象的一个输出方法

{

void show2();

}

class Car implements SS //汽车类

{

String logal;

public Car(String logal)

{

this.logal=logal;

}

public void show2()

{

System.out.print("\nlogal是 "+logal);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值