標題:
繼承 (三)
[打印本頁]
作者:
tonyh
時間:
2021-7-9 19:01
標題:
繼承 (三)
練習在子類別中添加新的特性,並在子類別中覆寫(override)自父類別繼承下來的方法。
public class Ch70 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal
{
String name;
int age;
double weight;
Animal(String n,int a,double w)
{
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
}
}
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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String n,int a,double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
陳宥穎
時間:
2021-7-9 20:10
public class Ch05 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"黑");
Dog d2=new Dog("球球",1,1.35,"橘");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal{
String name;
int age;
double weight;
Animal(String n, int a, double w)
{
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
}
}
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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤."+"毛色為"+color+"色");
}
}
class Cat extends Animal
{
Cat(String n, int a, double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
黃宥華
時間:
2021-7-9 20:11
public class CH01 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.show();
d1.sound(2);
d2.show();
d2.sound(3);
c1.show();
c1.sound(5);
}
}
class Animal {
String name;
int age;
double we;
Animal(String n,int a,double w){
name=n;
age=a;
we=w;
}
void show()
{
System.out.println(name+"今年"+age+"歲,體重"+we+"公斤");
}
}
class Dog extends Animal{
String col;
Dog(String n, int a, double w,String c) {
super(n, a, w);
col=c;
}
void sound(int x){
for(int i=1;i<=x;i++)
System.out.print("汪~~");
System.out.println();
}
void show()
{
System.out.println(name+"今年"+age+"歲,體重"+we+"公斤,毛色為"+col+"。");
}
}
class Cat extends Animal {
Cat(String n, int a, double w) {
super(n, a, w);
}
void sound(int x){
for(int i=1;i<=x;i++)
System.out.print("喵!");
System.out.println();
}
}
複製代碼
作者:
劉愷鈞
時間:
2021-7-9 20:11
public class Ch01 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal
{
String name;
int age;
double weight;
Animal(String n,int a, double w)
{
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.printf("%s今年%d歲,體重%.2f公斤%n",name,age,weight);
}
}
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=1;i<=x;i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.printf("%s今年%d歲,體重%f公斤,毛色為%s%n",name,age,weight,color);
}
}
class Cat extends Animal
{
Cat(String n,int a,double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1;i<=x;i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
蔡忻霓
時間:
2021-7-9 20:14
public class Ch13 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal
{
String name;
int age;
double weight;
Animal(String n,int a,double w)
{
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
}
}
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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String n,int a,double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
董宸佑
時間:
2021-7-9 20:15
public class Ch01 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,"+"毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String n, int a, double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
孫嘉駿
時間:
2021-7-9 20:16
public class Ch03 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String n,int a,double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
李宇澤
時間:
2021-7-9 20:16
public class Ch55 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal
{
String name;
int age;
double weight;
Animal(String n,int a,double w)
{
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
}
}
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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String n,int a,double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
林政瑜
時間:
2021-7-9 20:18
public class Ch02 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal{
String name;
int age;
double weight;
Animal(String n, int a, double w)
{
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
}
}
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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.毛色為"+color);
}
}
class Cat extends Animal
{
Cat(String n, int a, double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
夏子涵
時間:
2021-7-9 20:22
public class Ch70 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal
{
String name;
int age;
double weight;
Animal(String n,int a,double w)
{
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
}
}
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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String n,int a,double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
黃柏叡
時間:
2021-7-9 20:24
public class Ch3 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal
{
String name;
int age;
double weight;
Animal(String n,int a,double w)
{
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
}
}
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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String n,int a,double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
沈子晏
時間:
2021-7-16 18:53
public class Ch0709 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28,"棕色");
Dog d2=new Dog("球球",1,1.35,"白色");
Cat c1=new Cat("咪咪",3,0.95);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,"+"毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String n, int a, double w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2