最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

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

2019-12-17 14:05 作者:詩書畫唱  | 我要投稿


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í)行效果圖:


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

分享到微博請遵守國家法律
云和县| 泗阳县| 石阡县| 连云港市| 凭祥市| 郁南县| 九台市| 明星| 东明县| 蛟河市| 诸城市| 汝阳县| 宁强县| 郸城县| 德保县| 天镇县| 淮南市| 赫章县| 皋兰县| 波密县| 弥勒县| 诏安县| 桐城市| 舞阳县| 讷河市| 连江县| 岚皋县| 青阳县| 东乡县| 松原市| 义乌市| 阿鲁科尔沁旗| 阳山县| 厦门市| 从化市| 乐都县| 普陀区| 黄石市| 张家川| 塔河县| 永兴县|