Java oop代碼6:繼承實(shí)例(含原創(chuàng)方法:類比替代法)

1.
package a;
public class Student{?
public static void main(String[] args) {
System.out.println("貓類");
Cat c=new Cat();
c.color="黃色";
c.leg=4;
System.out.println(c.color+"? ?..."+c.leg);
c.eat();
c.sleep();
c.Catch();
System.out.println("狗類");
Dog d=new Dog();
d.color="黑色";
d.leg=4;
System.out.println(d.color+"? ..."+d.leg);
d.eat();
d.sleep();
? ? ? ? d.look();
}
}
class Animal {
String color; //動物的顏色
int leg; //動物腿的個(gè)數(shù)
public void eat() { //吃飯的功能
System.out.println("吃飯");
}
public void sleep() { //睡覺的功能
System.out.println("睡覺");
}
}//父類中構(gòu)造共有的方法
class Cat extends Animal {
public void Catch() {
System.out.println("貓捉老鼠");
}
}/*用extends來繼承,其中構(gòu)造的獨(dú)有方法在主函數(shù)里面這里用c.Catch();來引用*/
class Dog extends Animal {
public void look() {
System.out.println("狗看家");
}//用extends來繼承
}

2.
package a;
public class Student {
public static void main(String[] args) {
// 1、父類:交通工具:run方法
// 子類:公交車(Bus),自行車(Bike),飛機(jī)(Plane),地鐵
// (Metro)
feiji f=new feiji("飛機(jī)");
f.run();
ditie d=new ditie("地鐵");
d.run();
}
}
class jiaotonggongjv{
public String name;
public jiaotonggongjv(String name){
this.name=name;
}
public void run(){
System.out.println("執(zhí)行了"+this.name+"的run方法");
}
}
class gongjiaoche extends jiaotonggongjv{
public gongjiaoche(String name) {
super(name);
}
}
class zixingche extends jiaotonggongjv{
public zixingche(String name) {
super(name);
}
}
class feiji extends jiaotonggongjv{
public feiji(String name) {
super(name);
}
}
class ditie extends jiaotonggongjv{
public ditie(String name) {
super(name);
}
}
執(zhí)行效果圖:

2.用我原創(chuàng)的類比替代法(比如我把run全換成了walk)可得以下代碼:
package a;
public class Student {
public static void main(String[] args) {
feiji f=new feiji("老師");
f.walk();
ditie d=new ditie("學(xué)生");
d.walk();
ditie e=new ditie("藍(lán)領(lǐng)");
e.walk();
ditie s=new ditie("白領(lǐng)");
s.walk();
}
}
class person{
public String name;
public person(String name){
this.name=name;
}
public void walk(){
System.out.println("執(zhí)行了"+this.name+"的walk方法");
}
}
class gongjiaoche extends person{
public gongjiaoche(String name) {
super(name);
}
}
class zixingche extends person{
public zixingche(String name) {
super(name);
}
}
class feiji extends person{
public feiji(String name) {
super(name);
}
}
class ditie extends person{
public ditie(String name) {
super(name);
}
}
執(zhí)行效果圖:

3.用我原創(chuàng)的類比替代法(比如我把在2題得代碼中加上了綠字部分)可得以下代碼:
package a;
public class Student {
public static void main(String[] args) {
feiji f=new feiji("老師");
f.walk();
f.say();
ditie d=new ditie("學(xué)生");
d.walk();
d.play();
ditie e=new ditie("藍(lán)領(lǐng)");
e.walk();
e.gzzd();
ditie s=new ditie("白領(lǐng)");
s.walk();
s.gzg();
}
}
class person{
public String name;
public person(String name){
this.name=name;
}
public void walk(){
System.out.println("執(zhí)行了"+this.name+"的walk方法");
}
public void say(){
System.out.println("會說");
}
public void play(){
System.out.println("會玩");
}
public void gzzd(){
System.out.println("工資中等");
}
public void gzg(){
System.out.println("工資高");
}
}
class gongjiaoche extends person{
public gongjiaoche(String name) {
super(name);
}
}
class zixingche extends person{
public zixingche(String name) {
super(name);
}
}
class feiji extends person{
public feiji(String name) {
super(name);
}
}
class ditie extends person{
public ditie(String name) {
super(name);
}
}
執(zhí)行效果圖:
