標題:
繼承 (三)
[打印本頁]
作者:
tonyh
時間:
2022-9-17 19:21
標題:
繼承 (三)
練習在子類別中添加新的特性,並在子類別中覆寫(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();
}
}
複製代碼
作者:
余柏緯
時間:
2022-9-17 19:48
public class C500 {
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 a)
{
for(int i=1;i<=a;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 a)
{
for(int i=1;i<=a;i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
陳依彤
時間:
2022-9-17 19:48
package practice;
public class practice01 {
public static void main(String[] args) {
Dog d1= new Dog("憨憨",2,1.28,"棕色");
Dog d3=new Dog("球球",1,1.35,"白色");
Cat d2= new Cat("咪咪",3,0.95);
d1.show();
d1.makeSound(2);
d3.show();
d3.makeSound(3);
d2.show();
d2.makeSound(5);
}
}
class Function{
String name;
int age;
double weight;
Function(String a,int b,double c)
{
name=a;
age=b;
weight=c;
}
void show()
{
System.out.println(name+"今年"+age+"歲 "+weight+"公斤");
}
}
class Dog extends Function
{
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 show()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+".");
}
}
class Cat extends Function
{
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();
}
}
複製代碼
作者:
黃子倢
時間:
2022-9-17 19: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 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();
}
}
複製代碼
作者:
許志捷
時間:
2022-9-17 19:53
package lol;
public class Ch01 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.28f,"棕色");
Dog d2=new Dog("球球",1,1.35f,"白色");
Cat c1=new Cat("咪咪",3,0.95f);
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
}
}
class Animal{
String name;
int age;
float wgh;
Animal(String n,int a,float w)
{
name=n;
age=a;
wgh=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+wgh+"公斤.");
}
}
class Dog extends Animal
{
String color;
Dog(String n,int a,float 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+"歲,體重"+wgh+"公斤,毛色為"+color+".");
}
}
class Cat extends Animal
{
Cat(String n,int a,float w)
{
super(n,a,w);
}
void makeSound(int x)
{
for(int i=1;i<=x;i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
王秉鈞
時間:
2022-9-17 19:54
public class Ch33 {
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 weigth;
Animal(String n,int a,double w)
{
this.name=n;
this.age=a;
this.weigth=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲"+"體重"+weigth+"kg"+"毛色為");
}
}
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.println("尢");
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲"+"體重"+weigth+"kg"+"毛色為");
}
}
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.println("ㄇㄧㄠ");
System.out.println();
}
}
}
複製代碼
作者:
張博竣
時間:
2022-9-17 19:54
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();
}
}
複製代碼
作者:
呂尚霖
時間:
2022-9-17 20:01
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();
}
}
複製代碼
作者:
曾元瑜
時間:
2022-9-17 20:01
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=0;i<x;i++)
System.out.print("汪~");
System.out.println();
}
void showProfile(){
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+"。");
}
}
class Cat extends Animal{
String color;
Cat(String n,int a,double w){
super(n,a,w);
}
void makeSound(int x){
for(int j=0;j<x;j++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
丁肇志
時間:
2022-9-17 20:04
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 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=1; i<=x; i++)
System.out.print("汪~");
System.out.println();
}
void showProfile() {
System.out.println(name + "今年" + age + "歲,體重" + weight + "公斤,毛色為" + color + ".");
}
}
class Cat extends Animal {
String color;
Cat(String name, int age, double weight) {
super(name, age, weight);
}
void makeSound(int x) {
for(int i=1; i<=x; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
吳湘儀
時間:
2022-9-17 20:04
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();
}
}
複製代碼
作者:
許洧熏
時間:
2022-9-17 20:09
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=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 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();
}
}
複製代碼
作者:
王法棣
時間:
2022-9-17 20:10
package ch02;
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();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2