⑴ 编写一个完整的 java 程序,要求: (1)定义一个抽象类 Shape,在该类中:添加抽象方法
public abstract class Shape {
public abstract double Area();
public abstract void printArea();
}
class Circle extends Shape {
int radius;
public Circle(int radius) {
this.radius = radius;
}
@Override
public double Area() {
return Math.pow(this.radius, 2) * Math.PI;
}
@Override
public void printArea() {
// String.format("%.2f", Area()) 保留两位小数
System.out.println("半径为 " + this.radius + " 的圆的面积是 " + String.format("%.2f", Area()));
}
}
class Test {
public static void main(String[] args) {
// 生成一个 1 - 9 的随机整数
int radius = (int) (Math.random() * 9) + 1;
// 创建一个 Circle 对象实例
Circle circle = new Circle(radius);
// 调用 printArea() 方法打印面积
circle.printArea();
}
}
⑵ 浣跨敤java 瀹氫箟鎶借薄绫伙細public abstract class Person
浠g爜濡備笅銆備负浜嗘樉绀烘柟渚挎墍浠ュ啓鍦ㄥ悓涓涓鏂囦欢閲岄潰浜嗐
publicclassTest{
publicstaticvoidmain(String...args)throwsException{
Personstu1=newStudent();
stu1.preWork();
stu1.work();
stu1.reWork();
stu1.doHomework();
Persontea1=newTeacher();
tea1.preWork();
tea1.work();
tea1.reWork();
tea1.doHomework();
}
}
abstractclassPerson{
publicabstractvoidpreWork();
publicabstractvoidwork();
publicabstractvoidreWork();
publicabstractvoiddoHomework();
}
classStudentextendsPerson{
publicvoidpreWork(){
System.out.println("棰勪範");
}
publicvoidwork(){
System.out.println("涓婅");
}
publicvoidreWork(){
System.out.println("澶嶄範");
}
publicvoiddoHomework(){
System.out.println("鍐欎綔涓");
}
}
classTeacherextendsPerson{
publicvoidpreWork(){
System.out.println("澶囪");
}
publicvoidwork(){
System.out.println("涓婅");
}
publicvoidreWork(){
System.out.println("杈呭");
}
publicvoiddoHomework(){
System.out.println("鎵逛綔涓");
}
}
⑶ java绋嬪簭棰橈細瀹氫箟涓涓鎶借薄绫-姘存灉,鍏朵腑鍖呮嫭getWeight()鏂规硶,缂栧啓绋嬪簭鍒嗗埆鍒涘缓鑻规灉銆
姘存灉绫
abstractpublicclassFruit{
abstractpublicdoublegetWeight();
}
鑻规灉绫
publicclassAppleextendsFruit{
privatedoubleweight;
publicApple(doubleweight){
this.weight=weight;
}
@Override
publicdoublegetWeight(){
returnweight;
}
}
姗樺瓙绫
publicclassOrangeextendsFruit{
privatedoubleweight;
publicOrange(doubleweight){
this.weight=weight;
}
@Override
publicdoublegetWeight(){
returnweight;
}
}
妗冨瓙绫
publicclassPeachextendsFruit{
privatedoubleweight;
publicPeach(doubleweight){
this.weight=weight;
}
@Override
publicdoublegetWeight(){
returnweight;
}
}
涓荤被
publicclassMain{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Fruit[]fruits={newPeach(12),newApple(2),newOrange(5)};
for(Fruitfruit:fruits){
System.out.println(fruit.getClass().getName()+"鐨勯噸閲忔槸"
+fruit.getWeight());
}
}
}
杩愯岀粨鏋
Peach鐨勯噸閲忔槸 12.0
Apple鐨勯噸閲忔槸 2.0
Orange鐨勯噸閲忔槸 5.0
⑷ JAVA程序编写。 定义一个抽象类shape ,用来表示一般图形。
Java程序:
publicclassMain{
publicstaticvoidmain(String[]args){
Shapes=null;
s=newCircle(3);
System.out.println("圆的面积:"+s.area());
System.out.println("圆的周长:"+s.perimeter());
}
}
/**
*形状类:抽象类
*@authordeveloper
*@version2017.05.23
*/
abstractclassShape{
/**
*计算形状的面积
*@return形状的面积
*/
abstractdoublearea();
/**
*计算形状的周长
*@return形状的周长
*/
abstractdoubleperimeter();
}
/**
*圆类
*@authordeveloper
*@version2017.05.23
*/
classCircleextendsShape{
/**
*半径
*/
protecteddoubleradius;
/**
*构造方法
*@paramradius半径
*/
publicCircle(doubleradius){
this.radius=radius;
}
@Override
doublearea(){
returnMath.PI*radius*radius;
}
@Override
doubleperimeter(){
return2*Math.PI*radius;
}
}
运行测试:
圆的面积:28.274333882308138
圆的周长:18.84955592153876