⑴ 編寫一個完整的 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