Java初学6.3-继承
代码复用性差时可使用继承来提高代码的复用性问题。
当多个类存在相同的属性和方法时,可以从这些中从抽象出父类,在父类中定义这些相同的属性和方法,所有的子类不需要重新定义这些属性和方法,只需要通过关键字extends来声明继承父类即可。
格式:
class a extends b{
a(子类)会自动拥有b中的属性和方法。
}
package ExtendsTest;
public class Extends001 {
public static void main(String[] args) {
Extends003 x = new Extends003();
x.name="于江坤";
x.age=2024;
x.setScore(59);
x.Show001();
x.Show();
Extends004 d = new Extends004();
d.name="勺子长";
d.age=2023;
d.setScore(58);
d.Show002();
d.Show();
}
}
class Extends002 {//父类
public String name;
public int age;
private int Score;
//公共方法3个
public int getScore() {
return Score;
}
public void setScore(int score) {
this.Score = score;
}
public void Show(){
System.out.println("Age: " + age+", Score: " + Score);
}
}
//子类
class Extends003 extends Extends002 {
//特有的方法
public void Show001(){
System.out.println("------");
System.out.println("小帅哥: " + name);
}
}
class Extends004 extends Extends002 {
//特有的方法
public void Show002(){
System.out.println("------");
System.out.println("大帅哥:"+name);
}
}
子类继承了所有的属性和方法,但是私有的属性和方法不能在子类中直接访问,需要提供公共的方法访问。
package ExtendsTest;
public class ExtendsAccess {
public static void main(String[] args) {
//创建子类对象
ExtendsAccess002 E = new ExtendsAccess002();
E.test002();
//输出时同时也调用了父类的构造器,并且先调用父类。即子类必须调用父类的构造器,完成父类的初始化。
/*ExtendsAccess002 E1 = new ExtendsAccess002(1);//此时会再次调用父类的无参构造器
ExtendsAccess002 E2 = new ExtendsAccess002(1,2);*/
}
}
//父类
class ExtendsAccess001{
int a = 1;
public int b = 2;
protected int c = 3;
private int d = 4;
//私有属性d的公共方法
public int getD() {
return d;
}
//当出现多个构造器时,需要显示的调用。
public ExtendsAccess001(){//无参构造器
System.out.println("无参构造器!!!");
}
/*public ExtendsAccess001(String z){//无参构造器
System.out.println("一个参数的构造器!!!");
}
public ExtendsAccess001(String z,int x){//无参构造器
System.out.println("两个参数的构造器!!!");
}*/
void AA(){
System.out.println("AA");
}
public void BB(){
System.out.println("BB");
}
protected void CC(){
System.out.println("CC");
}
private void DD(){
System.out.println("DD");
}
public void DD001(){
DD();
}
}
//子类
class ExtendsAccess002 extends ExtendsAccess001{
public ExtendsAccess002(){//无参构造器
//隐藏了super();默认调用父类的无参构造器,当然也可以显示的写出来
//注意!!!!!!super必须写在第一行,否则会出现错误!!!!!!和this一样
//super("zxc");
// super("asd",18);
System.out.println("哈哈哈哈哈哈哈哈!");
}
//在创建子类对象时,无论使用的是哪个构造器,默认情况下总会调用父类的无参构造器
/*public ExtendsAccess002(int a){//ExtendsAccess002 E1 = new ExtendsAccess002(1);调用有参构造器
//super("zxc");
// super("asd",18);
System.out.println("嘻嘻~");
}*/
/*public ExtendsAccess002(int a,int b){//ExtendsAccess002 E2 = new ExtendsAccess002(1,2);调用有参构造器
//super("zxc");
//super("asd",18);
System.out.println("嘻嘻~");
}*/
public void test002(){
System.out.print("a:" + a + " b:"+ b + " c:"+ c);
//System.out.println("d:"+d);
//对于私有属性只能调用公共的方法来访问
System.out.println(" d:"+getD());
AA();
BB();
CC();
// DD();
//对于私有的方法DD只能通过公共的方法访问
DD001();
}
}
连续的继承(无参构造器):
package ExtendsTest;
//继承继承继承继承继承!!!
//子类最多只能继承一个父类,即单继承机制。
public class ExtendsInheritance {
public static void main(String[] args) {
ExtendsInheritance004 E = new ExtendsInheritance004();
E.ExtendsInheritance004();
}
}
class ExtendsInheritance001{
public void ExtendsInheritance001(){
System.out.println("ExtendsInheritance001");
}
}
class ExtendsInheritance002 extends ExtendsInheritance001 {
public void ExtendsInheritance002(){
super.ExtendsInheritance001();
System.out.println("ExtendsInheritance002");
}
}
class ExtendsInheritance003 extends ExtendsInheritance002{
public void ExtendsInheritance003(){
super.ExtendsInheritance002();
System.out.println("ExtendsInheritance003");
}
}
class ExtendsInheritance004 extends ExtendsInheritance003{
public void ExtendsInheritance004(){
super.ExtendsInheritance003();
System.out.println("ExtendsInheritance004");
}
}
连续继承(有参构造器);
package ExtendsTest;
public class ExtendsInheritance0001 {
public static void main(String[] args) {
ExtendsInheritance04 E = new ExtendsInheritance04("123",18,20.0, 30.0);
}
}
class ExtendsInheritance01 {
public ExtendsInheritance01() {
super();//调用Object中的无参构造器。
System.out.println("ExtendsInheritance01");
}
}
class ExtendsInheritance02 extends ExtendsInheritance01 {
public ExtendsInheritance02(String number) {
super();
System.out.println("ExtendsInheritance02");
}
}
class ExtendsInheritance03 extends ExtendsInheritance02{
public ExtendsInheritance03(String number, int i) {
super("1");
System.out.println("ExtendsInheritance03");
}
}
class ExtendsInheritance04 extends ExtendsInheritance03{
public ExtendsInheritance04(String number, int i, double d, double v) {
super("1",1);
System.out.println("ExtendsInheritance04");
}
}
小思考:
package ExtendsTest;
public class ExtendsNature {
public static void main(String[] args) {
Third third = new Third();
System.out.println(third.name);
//此时返回three,若出现private时,报错。若Third中没有name,则输出two。以此类推。
System.out.println(third.age);
System.out.println(third.sex);//由于存在继承,所以可以返回。
System.out.println(third.score);
}
}
class First{
String name="one";
int age=10;
double score=100;
}
class Second extends First{
String name="two";
int age=20;
int sex=1;
}
class Third extends Second{
String name="three";
int age=30;
}