栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java中的继承

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中的继承

1.继承是面向对象的三大特征之一,三大特征分别是:封装,继承和多态。
2.继承基本的作用是:代码复用。但是“重要的”作用是:有了继承才有了以后“方法的覆盖”和“多态机制”。
3.继承的语法格式:

	【修饰符】class 类名 extends 父类名{
	类体=属性+方法。
}

4.Java语言当中的继承只支持单继承,一个类不能继承很多类,只能继承一个类。
5.关于继承中的一些术语:
b类继承a类,其中:
a类称为:父类,基类,超类,superclass
b类称为:子类,派生类,subclass
6.子类继承父类的数据:

私有的不支持继承构造方法不支持继承其他都可以。
7.Java语言当中只支持单单元继承,但是一个类也可以间接继承其他类,例如:

C extends B{

}
B extends A{
}
A extends T{
}

c类直接继承B类,但是C类简介继承T、类,A类。
8.Java没有显示继承任何类,会自动继承JavaSE库中提供的Java.lang.Object类。

public class Account {
	private String actno;
	private double balance;

	public Account(String actno, double balance) {
		this.actno = actno;
		this.balance = balance;
	}
	public Account() {
	}
	public String getActno() {
		return actno;
	}
	public void setActno(String actno) {
		this.actno = actno;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	
}

public class CreditAccount {
	private String actno;
	private double balance;
	private double credit;
	public String getActno() {
		return actno;
	}
	public void setActno(String actno) {
		this.actno = actno;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public double getCredit() {
		return credit;
	}
	public void setCredit(double credit) {
		this.credit = credit;
	}
	public CreditAccount(String actno, double balance, double credit) {
		super();
		this.actno = actno;
		this.balance = balance;
		this.credit = credit;
	}
	public CreditAccount() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}

public class CreditAccount extends Account {
	private double credit;
	public double getCredit() {
		return credit;
	}
	public void setCredit(double credit) {
		this.credit = credit;
	}
	public CreditAccount(String actno, double balance, double credit) {
		super();
		this.credit = credit;
	}
	public CreditAccount() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}

public class ExtendsTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CreditAccount a=new CreditAccount();
		a.setActno("a-001");
		a.setBalance(1000);
		a.setCredit(0.99);
		System.out.println(a.getActno()+","+a.getBalance()+","+a.getCredit());
	}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736958.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号