// 继承也是存在缺点的:耦合度高,父类修改,子类受牵连。
// java只允许单继承。不允许多继承。java是简单的。C++支持多重继承。
public class ExtendsTest02 {
}
// 银行账户类
// 账户的属性:账号、余额
class Account2{
// 属性
private String actno;
private double balance;
//构造方法
public Account2() {
}
public Account2(String actno, double balance) {
this.actno = actno;
this.balance = balance;
}
//setter and getter
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;
}
}
// 其它类型的账户:信用卡账户
// 账号、余额、信誉度
class CreditAccount2 extends Account2{
private double credit;
//构造方法
public CreditAccount2(double credit) {
this.credit = credit;
}
public CreditAccount2(String actno, double balance, double credit) {
super(actno, balance);
this.credit = credit;
}
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
this.credit = credit;
}
}
子类继承父类,当发现父类的方法无法完成子类的业务需求时,需要重新对父类的方法进行覆盖重写。
//实现方法重写
public class OverrideTest02 {
public static void main(String[] args) {
//创建对象
People people =new People(“小明”);
//没有这样的构造方法,所以说构造方法不继承
//Chinese chinese =new Chinese(“小红”);
Chinese chinese =new Chinese();
American american =new American();
people.speak();//小明 is speaking
chinese.speak();//null is speaking Chinese
american.speak();//null is speaking English
}
}
class People {
//属性
private String name;
//构造方法
public People(){
}
public People(String name){
this.name = name;
}
//setter and getter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//方法
public void speak(){
System.out.println(this.getName() + " is speaking");
}
}
//中国人类
class Chinese extends People{
public void speak(){
System.out.println(this.getName() + " is speaking Chinese");
}
}
//美国人类
class American extends People{
public void speak(){
System.out.println(this.getName() + " is speaking English");
}
}
public class Test01 {
public static void main(String[] args) {
//父类型的引用指向子类型
//在编译阶段,编译器看这是一个Animal类型的对象,就去Animal的字节码文件看,发现其中有.move()的方法,所以编译通过
//在运行的时候发现真正参与运行是Fish类的,就执行Fish类的move()方法
总结
大型分布式系统犹如一个生命,系统中各个服务犹如骨骼,其中的数据犹如血液,而Kafka犹如经络,串联整个系统。这份Kafka源码笔记通过大量的设计图展示、代码分析、示例分享,把Kafka的实现脉络展示在读者面前,帮助读者更好地研读Kafka代码。
麻烦帮忙转发一下这篇文章+关注我
Animal类型的对象,就去Animal的字节码文件看,发现其中有.move()的方法,所以编译通过
//在运行的时候发现真正参与运行是Fish类的,就执行Fish类的move()方法
总结
大型分布式系统犹如一个生命,系统中各个服务犹如骨骼,其中的数据犹如血液,而Kafka犹如经络,串联整个系统。这份Kafka源码笔记通过大量的设计图展示、代码分析、示例分享,把Kafka的实现脉络展示在读者面前,帮助读者更好地研读Kafka代码。
麻烦帮忙转发一下这篇文章+关注我
[外链图片转存中…(img-i1r3aXOX-1714485534006)]