標題:
繼承 (二)
[打印本頁]
作者:
tonyh
時間:
2015-12-26 17:09
標題:
繼承 (二)
本帖最後由 tonyh 於 2016-1-9 11:58 編輯
範例程式碼中, 第40~45行與54~59行, 為子類別中新添加的特性.
public class ch73
{
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
{
Dog(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();
}
}
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:19
public class Ch73 {
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{
Dog(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();
}
}
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();
}
}
複製代碼
作者:
林宇翔
時間:
2015-12-26 17:19
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
{
Dog(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("");
}
}
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:20
class Ch73
{
public static void main(String args[])
{
Dog d1=new Dog("5643545345",-12342132,2e-10),d2=new Dog("A",200,2.434e-30);
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
{
Dog(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();
}
}
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:20
public class ch73
{
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
{
Dog(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();
}
}
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-29 19:14
public class ch67
{
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
{
Dog(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();
}
}
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();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2