按照题目要求编写的圆,圆锥和测试类的Java程序如下
Test.java文件内容如下
class Circle{
private double r
private String color
public Circle(double r){
this.r=r
}
public double area(){
return Math.PI*r*r
}
public double perimeter(){
return Math.PI*2*r
}
public double getR(){
return this.r
}
public void setR(double r){
this.r=r
}
public String getColor(){
return this.color
}
public void setColor(String color){
this.color=color
}
public String toString(){
return "圆的半径为"+r+",颜色为"+color
}
}
class Cone{
private Circle c
private double h
private String color
public Cone(Circle c,double h){
this.c=c
this.h=h
}
public double volume(){
return 1.0/3*c.area()*h
}
public Circle getCircle(){
return this.c
}
public void setCircle(Circle c){
this.c=c
}
public double getH(){
return this.h
}
public void setH(double h){
this.h=h
}
public String getColor(){
return this.color
}
public void setColor(String color){
this.color=color
}
public String toString(){
return "圆锥的底面积为"+c.area()+",高为"+h+",颜色为"+color
}
}
public class Test{
public static void main(String[] args){
Circle circle1=new Circle(2.5)
circle1.setColor("红色")
System.out.println(circle1.toString())
System.out.println("圆的面积为"+circle1.area())
System.out.println("圆的周长为"+circle1.perimeter())
Cone circlar1=new Cone(circle1,2.7)
circlar1.setColor("蓝色")
System.out.println(circlar1.toString())
System.out.println("圆锥的体积为"+circlar1.volume())
}
}
1、public static void main(String[] args)
2、public finall static
3、3
4、抽象类
5、implements
6、类:一个包含属性、方法,使用class关键字定义
对象:创建对象必须使用new关键字,对象其实在内存中存储的是类的引用地址。
7、try是可能发生异常的内容;
catch是发生异常后要处理的代码;
finally是始终执行的,比如关闭资源等。
8、
public class Demo {//小明今年12岁,他母亲比他大20岁。编程计算多少年后他母亲的年龄是小明的2倍。
public static void main(String[] args) {
System.out.println(getYeah())
}
public static int getYeah(){
int xmAge = 12,mqAge = 20,yeah = 0
while(true){
if(mqAge / 2 == xmAge){
return yeah
}
mqAge++
yeah++
}
}
}
9、
public class Demo {
public static void main(String[] args) {
new Thread(new Piao()).start()
new Thread(new Piao()).start()
new Thread(new Piao()).start()
new Thread(new Piao()).start()
}
}
class Piao implements Runnable{
private static int piao = 1
public void run(){
while(true){
synchronized(Piao.class){
if(piao > 100){
System.exit(0)
}
System.out.println(Thread.currentThread().getName()+"出票:" + piao + "号")
piao++
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)