標題:
繼承 (三)
[打印本頁]
作者:
tonyh
時間:
2015-12-26 17:31
標題:
繼承 (三)
本帖最後由 tonyh 於 2016-1-9 11:59 編輯
範例程式碼中, 第36行, 40行, 42~47行, 與60~65行, 為子類別中新添加的特性.
而第48~51行, 為在子類別中覆寫(override)自父類別繼承下來的方法.
public class ch74
{
public static void main(String args[])
{
Dog d1=new Dog("憨憨",2,1.28,"棕色");
d1.showProfile();
d1.makeSound(2);
Dog d2=new Dog("球球",1,1.35,"白色");
d2.showProfile();
d2.makeSound(3);
Cat c1=new Cat("咪咪",3,0.95);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal
{
String name;
int age;
double w;
Animal(String name, int age, double w)
{
this.name=name;
this.age=age;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
class Dog extends Animal
{
String color;
Dog(String name, int age, double w, String color)
{
super(name, age, w);
this.color=color;
}
void makeSound(int x)
{
for(int i=0; i<x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String name, int age, double w)
{
super(name, age, w);
}
void makeSound(int x)
{
for(int i=0; i<x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
李允軒
時間:
2015-12-26 17:40
[code]
public class Ch74 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
d1.showProfile();
d1.makeSound(2);
Dog d2=new Dog("球球",1,1.35,"白色");
d2.showProfile();
d1.makeSound(3);
Cat c1=new Cat("咪咪",3,0.95);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal{
String name;
int age;
double weight;
Animal(String name,int age,Double weight){
this.name=name;
this.age=age;
this.weight=weight;
}
void showProfile(){
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤。");
}
}
class Dog extends Animal{
String color;
Dog(String name,int age,Double weight,String color){
super(name,age,weight);
this.color=color;
}
void makeSound(int x){
for(int i=0; i<x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile(){
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color);
}
}
class Cat extends Animal{
Cat(String name,int age,Double weight){
super(name,age,weight);
}
void makeSound(int x){
for(int i=0; i<x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
[/code]
作者:
張峻瑋
時間:
2015-12-26 17:41
public class ch74
{
public static void main(String args[]){
Dog d1=new Dog("憨憨",2,1.28,"棕色");
d1.showProfile();
d1.makeSound(50);
Dog d2=new Dog("球球",1,1.35,"白色");
d2.showProfile();
d2.makeSound(20);
Cat c1=new Cat("咪咪",3,0.95);
c1.showProfile();
c1.makeSound(100);
}
}
class Animal
{
String name;
int age;
double w;
Animal(String name,int age,double w)
{
this.name=name;
this.age=age;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
class Dog extends Animal
{
String color;
Dog(String name,int age,double w,String color)
{
super(name,age,w);
this.color=color;
}
void makeSound(int x)
{
for(int i=0;i<x;i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String name,int age,double w)
{
super(name,age,w);
}
void makeSound(int x)
{
for(int i=0;i<x;i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
劉得恩
時間:
2015-12-26 17:42
class Ch74
{
public static void main(String args[])
{
Dog d1=new Dog("5643545345",-12342132,2e-10,""),d2=new Dog("A",200,2.434e-30/0.0,"d");
d1.showProfile(); d1.makeSound(1);
d2.showProfile(); d2.makeSound(10);
Cat c=new Cat("€",-2,0.000000000001);
c.showProfile(); c.makeSound(2000) ;
}
}
class Animal
{
String name;
int age;
double w;
Animal(String n,int a,double w)
{
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤");
}
}
class Dog extends Animal
{
String color;
Dog(String n,int a,double w,String c)
{
super(n, a, w) ;
color=c;
}
void makeSound(int x)
{
for(int i=0;i<x;i++)
{
System.out.print("汪~");
}
System.out.println();
}
void showProfile()
{
super.showProfile();
System.out.println("毛色為"+color);
}
}
class Cat extends Animal
{
Cat(String n,int a,double w )
{
super(n, a, w) ;
}
void makeSound(int x)
{
for(int i=0;i<x;i++)
{
System.out.print("喵~");
}
System.out.println();
}
}
複製代碼
作者:
林宇翔
時間:
2015-12-26 17:46
[code]public class CH72
{
public static void main(String args[])
{
Dog d1=new Dog("憨憨",2,1.28,"棕色");
d1.showProfile();
d1.makeSound(2);
Dog d2=new Dog("球球",1,1.35,"白色");
d2.showProfile();
d2.makeSound(3);
Cat c1=new Cat("咪咪",3,0.95);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal
{
String name;
int age;
double w;
Animal(String name, int age, double w)
{
this.name=name;
this.age=age;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
class Dog extends Animal
{
String color;
Dog(String name, int age, double w, String color)
{
super(name, age, w);
this.color=color;
}
void makeSound(int x)
{
for(int i=0; i<x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String name, int age, double w)
{
super(name, age, w);
}
void makeSound(int x)
{
for(int i=0; i<x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
[/code]
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2