Java oop代碼6(原創(chuàng)方法):5道用上extends繼承,子類,父類的題

//1、父類:交通工具:run方法
子類:公交車(Bus),自行車(Bike),飛機(Plane),地鐵?(Metro)
package a;
public class Student {
public static void main(String[] args) {
feiji f=new feiji("飛機");
f.run();
f.fly();
ditie d=new ditie("地鐵");
d.run();
d.zr();
feiji s=new feiji("公交車");
s.run();
s.zr();
feiji z=new feiji("自行車");
z.run();
z.znpy();
}
}
class jiaotonggongjv{
public String name;
public jiaotonggongjv(String name){
this.name=name;
}//紅色部分是我認為重要或需記憶或不熟或可能會忘的部分
public void run(){
System.out.println("執(zhí)行了"+this.name+"的run方法");
}
public void zr(){
System.out.println("執(zhí)行了"+this.name+"的載人方法");
}
public void fly(){
System.out.println("執(zhí)行了"+this.name+"的飛方法");
}
public void znpy(){
System.out.println("執(zhí)行了"+this.name+"的載女朋友方法");
}
}
class gongjiaoche extends jiaotonggongjv{
public gongjiaoche(String name) {
super(name);
}
}
class zixingche extends jiaotonggongjv{
public zixingche(String name) {
super(name);
}
}
class feiji extends jiaotonggongjv{
/*因為class feiji 繼承了class jiaotonggongjv,所以class?jiaotonggongjv里面的
public String name;
public jiaotonggongjv(String name){
this.name=name;
的代碼作用,class feiji也有了,所以前面可寫出feiji f=new feiji("飛機");,也可寫成
jiaotonggongjv f=new jiaotonggongjv("飛機");讓前面System.out.println("執(zhí)行了"+this.name+"的飛方法");中的this.name可以打印出"飛機"
*/
public feiji(String name) {
super(name);//這里就有可少寫些name代碼的作用了,"飛機"會賦值到name與this.name中
}
}
class ditie extends jiaotonggongjv{
public ditie(String name) {
super(name);
}
}
執(zhí)行效果圖:

//2、父類:人:walk方法
//子類:老師,學生,藍領,白領
package a;
public class Student {
public static void main(String[] args) {
person f=new person("老師");
f.walk();
f.say();
person d=new person("學生");
d.walk();
d.play();
person e=new person("藍領");
e.walk();
e.gzzd();
person s=new person("白領");
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);
}
}

3、父類:課程:exam方法
子類:語文,數(shù)學,英語
package a;
public class Student {
public static void main(String[] args) {
feiji f=new feiji("語文");
f.exam();
f.fly();
ditie d=new ditie("數(shù)學");
d.exam();
d.zr();
feiji s=new feiji("英語");
s.exam();
s.zx();
}
}
class kecheng{
public String name;
public kecheng(String name){
this.name=name;
}
public void exam(){
System.out.println("執(zhí)行了"+this.name+"的exam方法");
}
public void zr(){
System.out.println("執(zhí)行了"+this.name+"的變得聰明的方法");
}
public void fly(){
System.out.println("執(zhí)行了"+this.name+"的變得有涵養(yǎng)的方法");
}
public void zx(){
System.out.println("執(zhí)行了"+this.name+"的變得自信的方法");
}
}
class gongjiaoche extends kecheng{
public gongjiaoche(String name) {
super(name);
}
}
class zixingche extends kecheng{
public zixingche(String name) {
super(name);
}
}
class feiji extends kecheng{
public feiji(String name) {
super(name);
}
}
class ditie extends kecheng{
public ditie(String name) {
super(name);
}
}

4、父類:形狀:pait方法
子類:三角形,正方形,圓形,五角星
package a;
public class Student {
public static void main(String[] args) {
sjx f=new sjx("三角形");/*這里sjx(子類名)換為person(父類名),雖然可少寫些extends部分,但就沒太多意義了,只是單純的方法調用了,因為這題是為了多練熟extends部分,所以不換為person,這里寫了相應的extends部分,才可xie相應的 子類名? 名=new??子類名("? ? ?")*/
f.pait();
f.stb();
zfx d=new zfx("正方形");
d.pait();
d.sitb();
yx e=new yx("圓形");
e.pait();
e.ka();
wjx s=new wjx("五角星");
s.pait();
s.wgj();
}
}
class person{
public String name;
public person(String name){
this.name=name;
}
public void pait(){
System.out.println("執(zhí)行了"+this.name+"的pait方法");
}
public void stb(){
System.out.println("3條邊");
}
public void sitb(){
System.out.println("4條邊");
}
public void wgj(){
System.out.println("5個角");
}
public void ka(){
System.out.println("圓圓的很可愛,讓人有視覺享受");
}
}
class sjx extends person{
public sjx (String name) {
super(name);
}
}
class zfx extends person{
public zfx(String name) {
super(name);
}
}
class yx extends person{
public yx(String name) {
super(name);
}
}
class wjx extends person{
public wjx(String name) {
super(name);
}
}

5、父類:英雄:cast方法
子類:法師、戰(zhàn)士、刺客、獵人、牧師
package a;
public class Student {
public static void main(String[] args) {
sjx f = new sjx("法師");
f.cast();
f.mf();
zfx d = new zfx("戰(zhàn)士");
d.cast();
d.jz();
yx e = new yx("刺客");
e.cast();
e.as();
wjx s = new wjx("獵人");
s.cast();
s.sl();
MS j = new MS("牧師");
j.cast();
j.xrsxt();
}
}
class person {
public String name;
public person(String name) {
this.name = name;
}
public void cast() {
System.out.println("執(zhí)行了" + this.name + "的cast方法");
}
public void jz() {
System.out.println("近戰(zhàn)");
}
public void xrsxt() {
System.out.println("送人上西天");
}
public void mf() {
System.out.println("魔法");
}
public void as() {
System.out.println("暗殺");
}
public void sl() {
System.out.println("狩獵");
}
}
class sjx extends person {
public sjx(String name) {
super(name);
}
}
class zfx extends person {
public zfx(String name) {
super(name);
}
}
class yx extends person {
public yx(String name) {
super(name);
}
}
class wjx extends person {
public wjx(String name) {
super(name);
}
}
class MS extends person {
public MS(String name) {
super(name);
}
}//注意不要套在class wjx等類中去了
