標題:
繼承 (三)
[打印本頁]
作者:
tonyh
時間:
2019-9-9 20:23
標題:
繼承 (三)
練習在子類別中添加新的特性,並在子類別中覆寫(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();
}
}
複製代碼
作者:
洪翊庭
時間:
2019-9-9 20:47
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();
}
}
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();
}
}
複製代碼
作者:
李沛昂
時間:
2019-9-9 20:48
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 showProfile(){
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+"色.");
}
void makeSound(int n)
{
for(int i=1;i<=n;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 n)
{
for(int i=1;i<=n;i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
黃宇綸
時間:
2019-9-9 20:51
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.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();
}
}
複製代碼
作者:
黃宇瑄
時間:
2019-9-9 20:52
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();
}
}
複製代碼
作者:
洪翊展
時間:
2019-9-9 20:55
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 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 showProfile(){
System.out.println(name+"今年"+age+"歲,體重為"+w+"公斤,毛色為"+color+"色");
}
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();
}
}
複製代碼
作者:
趙一鳴
時間:
2019-9-9 20:57
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 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 showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+"色");
}
void makeSound(int s)
{
for(int i=1;i<=s;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 s)
{
for(int i=1;i<=s;i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
戴嘉禾
時間:
2019-9-9 20:58
public class Jiaho {
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();
}
}
複製代碼
作者:
may
時間:
2019-9-12 21:38
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)//定義Dog子類別的建構子
{
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();
}
}
作者:
鄭楀諺
時間:
2019-9-13 11:10
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();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2