Java进阶学习-4 继承


title: Java进阶学习-4 继承
date: 2020-02-02 10:51:59
tags: Java学习

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JXR7WuOg-1580663827442)(C:\Users\36987\AppData\Roaming\Typora\typora-user-images\image-20200202121516141.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wiXGbdvm-1580663827446)(C:\Users\36987\AppData\Roaming\Typora\typora-user-images\image-20200202121525645.png)]

最初版本

Database.java

package dome;

import java.util.ArrayList;

public class Database {
	private ArrayList<CD> listCD = new ArrayList<CD>();
	private ArrayList<DVD> listDVD = new ArrayList<DVD>();
	
	public void add(CD cd) {
		listCD.add(cd);
	}
	
	public void add(DVD dvd) {
		listDVD.add(dvd);
	}
	
	public void list() {
		for( CD cd : listCD ) {
			cd.print();
		}
		for( DVD dvd : listDVD ) {
			dvd.print();
		}
	}
	public static void main(String[] args) {
		Database db = new Database();
		db.add(new CD("abc", "abc", 4, 60, "..."));
		db.add(new CD("def", "def", 4, 60, "..."));
		db.add(new DVD("xxx", "aaa", 60, "..."));
		db.list();
	}

}

CD.java

package dome;

public class CD {
	private String title;
	private String artist;
	private int numofTracks;
	private int playingTime;
	private boolean gotIt = false;
	private String comment;
	
	public CD(String title, String artist, int numofTracks, int playingTime, String comment) {
//		super();
		this.title = title;
		this.artist = artist;
		this.numofTracks = numofTracks;
		this.playingTime = playingTime;
		this.comment = comment;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}


	public void print() {
		// TODO Auto-generated method stub
		System.out.println("CD:"+title + ":" + artist);
	}

}

DVD.java

package dome;

public class DVD {
	private String title;
	private String director;
	private int playingTime;
	private boolean gotIt = false;
	private String comment;
	
	public DVD(String title, String director, int playingTime, String comment) {
		super();
		this.title = title;
		this.director = director;
		this.playingTime = playingTime;
		this.comment = comment;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	public void print() {
		System.out.println("DVD"+title + ":" + director);
	}

}

但是存在很多的代码复制 代码质量不高后期维护会很费劲。

CD与DVD类十分的相似 拷贝粘贴 且不具有可扩展性。如果新加MP3 类所有地方都要加上对应的功能。

故创建一个Item CD与DVD为Item的派生

代码变化为:

Database.java

package dome;

import java.util.ArrayList;

public class Database {
//	private ArrayList<CD> listCD = new ArrayList<CD>();
//	private ArrayList<DVD> listDVD = new ArrayList<DVD>();
	private ArrayList<Item> listItem = new ArrayList<Item>();
	
//	public void add(CD cd) {
//		listCD.add(cd);
//	}
//	
//	public void add(DVD dvd) {
//		listDVD.add(dvd);
//	}
	public void add(Item item) {
		listItem.add(item);
	}
	
	public void list() {
//	public void list() {
//		for( CD cd : listCD ) {
//			cd.print();
//		}
//		for( DVD dvd : listDVD ) {
//			dvd.print();
//		}
//	}
		for( Item item : listItem ) {
			item.print();
		}
	}
	
	public static void main(String[] args) {
		Database db = new Database();
		db.add(new CD("abc", "abc", 4, 60, "..."));
		db.add(new CD("def", "def", 4, 60, "..."));
		db.add(new DVD("xxx", "aaa", 60, "..."));
		db.list();
	}

}

CD.java :

package dome;

public class CD extends Item {
	private String title;
	private String artist;
	private int numofTracks;
	private int playingTime;
	private boolean gotIt = false;
	private String comment;
	
	public CD(String title, String artist, int numofTracks, int playingTime, String comment) {
//		super();
		this.title = title;
		this.artist = artist;
		this.numofTracks = numofTracks;
		this.playingTime = playingTime;
		this.comment = comment;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CD cd = new CD("a", "b", 2, 2, "...");
		cd.print();
	}


//	public void print() {
//		// TODO Auto-generated method stub
//		System.out.println("CD:"+title + ":" + artist);
//	}

}

DVD.java

package dome;

public class DVD extends Item {
	private String title;
	private String director;
	private int playingTime;
	private boolean gotIt = false;
	private String comment;
	
	public DVD(String title, String director, int playingTime, String comment) {
		super();
		this.title = title;
		this.director = director;
		this.playingTime = playingTime;
		this.comment = comment;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	public void print() {
		System.out.println("DVD"+title + ":" + director);
	}

}

Item.java

package dome;

public class Item {

	public void print() {
		System.out.println("Item");
	}
}

此时输出为:

Item
Item
DVDxxx:aaa

代表了 所有继承Item类中的类 都有Item中的函数。

如果自己没有Item中的函数则使用item中的函数。

如果自己有与Item中重名的函数 则使用自己的函数。

且在Database.java中不直接使用Database管理CD DVD而是使用Item进行输出。

Item为父类在调试的时候

CD.java单独调试 做print()的时候跳入函数为Item.java 天然继承得到了cd.print();

父类的所有东西都被继承下来,都有可能被访问

子类 extends 父类 获得父类的所有东西 然后可以增加新的东西。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oyuICKF4-1580663827451)(C:\Users\36987\AppData\Roaming\Typora\typora-user-images\image-20200202134542944.png)]

父类的内容都继承下来 private不可以直接用

①改为protected 使得所有本类的可以访问父类protected内容 但是其他包不可访问

这样做的问题是有可能父类不在同一个包内。

②使用super();使得在引用的地方 使用super(父类的private函数)

父类要构造一个新的生成初始化的函数。

父类

package dome;

public class Item {
	private String title;
	
	public Item(String title) {
		super();
		this.title = title;
	}

	public void print() {
		System.out.println("Item");
	}

}

子类CD

public class CD extends Item {
	private String artist;
	private int numofTracks;
	private int playingTime;
	private boolean gotIt = false;
	private String comment;
	
	public CD(String title, String artist, int numofTracks, int playingTime, String comment) {
		super(title);	//父类做
		// 自己不用做这个this.title = title;
		this.artist = artist;
		this.numofTracks = numofTracks;
		this.playingTime = playingTime;
		this.comment = comment;
	}

如果父类没有传递参数给子类 那么子类在调用构造函数的时候会跳转到父类中的没有调用参数的函数,如果父类有传参数则会调用构造函数super先进构造函数再调用super(参数)入父类传参回构造函数运行

如果要使用父类的参数,①父类要有恰当的初始化②构造器generate

父类的构造器会先做 再做子类的构造器。如果没有super找的是没有参数的构造器 如果有super则会找有参数的构造器。

当父类和子类都有同一个变量的时候 系统会有两个同名成员变量 在使用的时候 父类的成员变量就被隐藏 在子类使用的时候 使用的是子类自己的成员变量 在父类的时候父类只用自己的成员变量。

变量在使用的时候遵循就近原则

总结:

父类都继承给子类。

但是父类的东西为private 子类不能碰 但是可以通过父类的函数去碰 在谁的函数里头 成员函数就是谁的

子类和父类出现同名变量,子类的同名变量就是指子类自己的成员变量 父类所指的同名变量就是父类自己的成员变量 之间没有联系

package dome;

import java.util.ArrayList;

public class Database {
//	private ArrayList<CD> listCD = new ArrayList<CD>();
//	private ArrayList<DVD> listDVD = new ArrayList<DVD>();
	private ArrayList<Item> listItem = new ArrayList<Item>();
	
//	public void add(CD cd) {
//		listCD.add(cd);
//	}
//	
//	public void add(DVD dvd) {
//		listDVD.add(dvd);
//	}
	public void add(Item item) {
		listItem.add(item);
	}
	
	public void list() {
//	public void list() {
//		for( CD cd : listCD ) {
//			cd.print();
//		}
//		for( DVD dvd : listDVD ) {
//			dvd.print();
//		}
//	}
		for( Item item : listItem ) {
			item.print();
		}
	}
	
	public static void main(String[] args) {
		Database db = new Database();
		db.add(new CD("abc", "abc", 4, 60, "..."));
		db.add(new CD("def", "def", 4, 60, "..."));
		db.add(new DVD("xxx", "aaa", 60, "..."));
		db.list();
	}

}

DVD:

package dome;

public class DVD extends Item {
	private String director;
	
	public DVD(String title, String director, int playingTime, String comment) {
		super(title, playingTime, false, comment);
		setTitle("b");
		this.director = director;
	}
	public static void main(String[] args) {
		DVD dvd = new DVD("a", "b", 1, "..");
		dvd.print();
	}
	public void print() {
		
		System.out.print("DVD:");
		super.print();		//去父类调用print()
		System.out.print(director);
	}

}

Item

package dome;

public class Item {
	private String title;
	private int playingTime;
	private boolean gotIt = false;
	private String comment;
	
	public Item() {
		
	}
	
	public Item(String title, int playingTime, boolean gotIt, String comment) {
		super();
		this.title = title;
		this.playingTime = playingTime;
		this.gotIt = gotIt;
		this.comment = comment;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	public void print() {
		System.out.print(title);
	}

}

DVD调试结果:

DVD:bb

有一个b为父类中输出的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值