標題:
繼承 (二)
[打印本頁]
作者:
tonyh
時間:
2018-11-3 15:15
標題:
繼承 (二)
本帖最後由 tonyh 於 2021-6-29 17:19 編輯
試在子類別中添加新的方法成員.
public class Ch69 {
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
{
Dog(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();
}
}
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();
}
}
複製代碼
作者:
蔡幸融
時間:
2018-11-3 15:30
public class Ch68 {
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;
float weight;
Animal(String n, int a, float w)
{
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲, 體重"+weight+"工斤");
}
}
class Dog extends Animal
{
Dog(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();
}
}
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();
}
}
}
複製代碼
作者:
蕭澧邦
時間:
2018-11-3 15:30
public class Ch69 {
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
{
Dog(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();
}
}
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();
}
}
複製代碼
作者:
黃宥鈞
時間:
2018-11-10 14:13
public class Ch69 {
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
{
Dog(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();
}
}
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();
}
}
複製代碼
作者:
譚暐霖
時間:
2018-11-10 14:14
public class Ch69 {
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
{
Dog(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();
}
}
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();
}
}
複製代碼
作者:
蔡幸融
時間:
2018-11-10 14:15
public class Ch69
{
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(3);
d2.showProfile();
d2.makeSound(6);
c1.showProfile();
c1.makeSound(2);
}
}
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
{
Dog(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();
}
}
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();
}
}
複製代碼
作者:
洪榜蔓
時間:
2018-11-10 14:16
public class Ch69 {
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
{
Dog(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();
}
}
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();
}
}
複製代碼
作者:
莊旻叡
時間:
2018-12-1 12:38
public class Ch69 {
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
{
Dog(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();
}
}
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://seed.istak.org.tw/)
Powered by Discuz! 7.2