標題:
繼承 (二)
[打印本頁]
作者:
tonyh
時間:
2017-2-24 20:54
標題:
繼承 (二)
本帖最後由 tonyh 於 2019-9-2 18:31 編輯
試在子類別中添加新的方法成員.
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();
}
}
複製代碼
作者:
沈子耕
時間:
2017-2-24 21:09
public class Ch68 {
public static void main(String[] args) {
Dog d1=new Dog("旺旺",8,45);
Dog d2=new Dog("球球",9,40);
Cat c1=new Cat("咪咪",2,10);
d1.viewProfile();
d2.viewProfile();
c1.viewProfile();
d1.viewProfile();
d1.makeSound(2);
d2.viewProfile();
d2.makeSound(3);
c1.viewProfile();
c1.makeSound(5);
}
}
class Animal{
int age;
String name;
double weight;
Animal(String n,int a, int w){
name=n;
age=a;
weight=w;
}
public void viewProfile(){
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
}
}
class Dog extends Animal{
Dog(String n,int a, int 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, int w){
super(n,a,w);
}
void makeSound(int x){
for(int i=0; i<x; i++){
System.out.print("喵~");
}
System.out.println("");
}
}
複製代碼
作者:
洪振庭
時間:
2017-2-24 21:09
public class Ch69 {
public static void main(String[] args) {
Dog dd=new Dog("阿肥",1,1.28);
Dog db=new Dog("皮球",2,1.35);
Cat cc=new Cat("骷髏",3,0.95);
dd.show();
dd.sounds(2);
db.show();
db.sounds(3);
cc.show();
cc.sounds(5);
}
}
class Animal{
String name;
int age;
double weight;
Animal(String n,int a,double w){
name=n;
age=a;
weight=w;
}
void show(){
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
}
}
class Dog extends Animal{
Dog(String n,int a,double w){
super(n,a,w);
}
void sounds(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 sounds(int n){
for(int i=1;i<=n;i++)
System.out.print("喵~");
System.out.println();
}
複製代碼
作者:
曾挺桂
時間:
2017-2-24 21:17
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();
}
}
複製代碼
作者:
黃璽安
時間:
2017-2-26 21:28
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.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();
}
}
}
複製代碼
作者:
李知易
時間:
2017-3-3 20:10
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();
}
}
複製代碼
作者:
陳思惟
時間:
2017-3-3 20:43
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