找回密码
 立即注册
本季方案:
本季讲解了JAVA中承继产生的因素及承继的基本实现,以后本季又要点论述了JAVA中关于承继实现的各种限制,包括子类目标的实例化进程,方法的覆写、super关键词的使用等。
类的承继
子承父业,假如孩子不是个败家子,最少可以坚持家业不败,假如孩子上进,则家业必定越来越兴盛。
调查以下一种状况:
·Person类:name属性、age属性、setter、getter
·Student类:name属性、age属性、school属性、setter、getter
Student是一个人吧?Student与Person类比较,发现只多了一个School属性。假如定义成两个类必定是糟蹋的,最少Student类中的name和age是无用的。我们可以把Student类作为Person类来管理。
classPerson
{
Stringname;
intage;
publicStringgetInfo()
{
return名字:+this.name+,年纪:+this.age;
}
};
//Student类与Person类比较是扩展了Person类的功能,所以此处承继即可
classStudentextendsPerson
{
//假如此处任何内容都不写,则最少应当与Person类的内容共同
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
//使用子类目标
Students=newStudent();
//以下的全面操作都是在Person类中定义的
s.name=王乾;
s.age=27;
System.out.println(s.getInfo());
}
};
哪怕子类没有添加任何新的工具,则最少也可以从父类承继来很多的内容。下面显示的即是Student类承继Person类调用getInfo()方法。
那么子类实际上也可以扩展父类的功能,此时只需要像之前那样直接在子类中定义即可我们看下面Demo02。
classPerson
{
Stringname;
intage;
publicStringgetInfo()
{
return名字:+this.name+,年纪:+this.age;
}
};
//Student类与Person类比较是扩展了Person类的功能,所以此处承继即可
classStudentextendsPerson
{
//假如此处任何内容都不写,则最少应当与Person类的内容共同
Stringschool;
};
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
//使用子类目标
Students=newStudent();
//以下的全面操作都是在Person类中定义的
s.name=王乾;
s.age=27;
//在Student类中添加的school属性
s.school=江南大学;
System.out.println(s.getInfo()+,校园:+s.school);
}
};
我们来验证一下哈,的确现已扩展了类滴功能哈,这即是承继的优点哈~
承继的规则
现实生活,一个孩子只能有一个亲爸,一个子类只能有一个父类。而不能有多个父类(实际上此概念是从C++来滴,C++允许子类有多个父类哈~~~)。可是一个父类可以有多个子类哈~~~
承继的规则
子类承继的时候是直接承继了父类中的非私有属性和非私有方法,而隐含的承继了父类中的私有属性(子类通过接口可以访问父类的私有属性和私有方法)。
老头子=gt;财物给孩子了=gt;孩子可以通过老头子的钥匙打开保险柜哈~
=gt;情书(保密的,私有的)=gt;保险柜
classPerson
{
privateStringname;
privateintage;
//私有属性要参加setter和getter操作就可以访问了哈~
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicStringgetInfo()
{
return名字:+this.name+,年纪:+this.age;
}
};
//Student类与Person类比较是扩展了Person类的功能,所以此处承继即可
classStudentextendsPerson
{
//假如此处任何内容都不写,则最少应当与Person类的内容共同
Stringschool;
publicvoidfun()
{
setName(王乾);
setAge(27);
}
publicvoidprint(){
System.out.println(getInfo()+,校园:+school);
}
};
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
//使用子类目标
Students=newStudent();
}
};
我们发现没有报错哈,通过setter和getter方法,子类就可以承继父类的私有属性name和age
私有属性可以在子类中直接通过setter和getter方法获得,所以关于私有的内容实际上应当是提供操作的出口的。
子类目标实例化的进程
我们来验证一下是不是子类目标在实例化时先调用父类的无参结构方法,然后再调用子类的结构方法,我们来看Demo04
classPerson
{
privateStringname;
privateintage;
publicPerson()
{
System.out.println(
父类的结构方法
);
}
//私有属性要参加setter和getter操作就可以访问了哈~
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicStringgetInfo()
{
return名字:+this.name+,年纪:+this.age;
}
};
//Student类与Person类比较是扩展了Person类的功能,所以此处承继即可
classStudentextendsPerson
{
publicStudent()
{
System.out.println(
子类的结构方法
);
}
//假如此处任何内容都不写,则最少应当与Person类的内容共同
Stringschool;
publicvoidfun()
{
setName(王乾);
setAge(27);
[零基础学JAVA]JavaSE面向目标部分-13.面向目标高档(01).doc

(Program this season:
This season explains the factors of inheritance in JAVA and the basic implementation of inheritance. Later, this season will discuss various limitations of inheritance in JAVA, including the instantiation process of subclass targets, method overriding, super keyword use, etc.
class inheritance
If the child inherits the father's business, if the child is not a prodigal, at least the family business can be undefeated. If the child is motivated, the family business will become more and more prosperous.
Investigate one of the following conditions:
· Person class: name attribute, age attribute, setter, getter
·Student class: name attribute, age attribute, school attribute, setter, getter
Student is a person, right? Compared with the Person class, Student has only one more School property. If it is defined as two classes, it must be wasteful, at least the name and age in the Student class are useless. We can manage the Student class as the Person class.
classPerson
{
Stringname;
intage;
publicStringgetInfo()
{
return name:  this.name , age:  this.age;
}
};
//The comparison between the Student class and the Person class extends the function of the Person class, so it can be inherited here
classStudentextendsPerson
{
//If nothing is written here, it should be at least shared with the content of the Person class
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
// use subclass target
Students=newStudent();
//The following comprehensive operations are defined in the Person class
s.name=Wang Gan;
s.age=27;
System.out.println(s.getInfo());
}
};
Even if the subclass does not add any new tools, at least it can inherit a lot from the parent class. The following shows that the Student class inherits the Person class and calls the getInfo() method.
Then the subclass can actually extend the function of the parent class. At this time, it only needs to be defined directly in the subclass as before. Let's see Demo02 below.
classPerson
{
Stringname;
intage;
publicStringgetInfo()
{
return name:  this.name , age:  this.age;
}
};
//The comparison between the Student class and the Person class extends the function of the Person class, so it can be inherited here
classStudentextendsPerson
{
//If nothing is written here, it should be at least shared with the content of the Person class
Stringschool;
};
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
// use subclass target
Students=newStudent();
//The following comprehensive operations are defined in the Person class
s.name=Wang Gan;
s.age=27;
//The school attribute added in the Student class
s.school=Jiangnan University;
System.out.println(s.getInfo() , campus: s.school);
}
};
Let's verify it, indeed, the drop-like function has been extended, which is the advantage of inheritance~
Inheritance rules
In real life, a child can only have one father, and a subclass can only have one parent. And cannot have multiple parent classes (in fact, this concept comes from C  , C   allows subclasses to have multiple parent classes~~~). But a parent class can have multiple subclasses~~~
Inheritance rules
When a subclass inherits, it directly inherits the non-private properties and non-private methods in the parent class, and implicitly inherits the private properties in the parent class (the subclass can access the parent class's private properties and private methods through the interface).
The old man=gt; the property is given to the child=gt; the child can open the safe through the old man's key~
=gt;love letter (confidential, private)=gt;safe
classPerson
{
privateStringname;
privateintage;
//Private properties can be accessed by participating in setter and getter operations~
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicStringgetInfo()
{
return name:  this.name , age:  this.age;
}
};
//The comparison between the Student class and the Person class extends the function of the Person class, so it can be inherited here
classStudentextendsPerson
{
//If nothing is written here, it should be at least shared with the content of the Person class
Stringschool;
publicvoidfun()
{
setName(Wang Gan);
setAge(27);
}
publicvoidprint(){
System.out.println(getInfo() , campus: school);
}
};
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
// use subclass target
Students=newStudent();
}
};
We found that there is no error, through the setter and getter methods, the subclass can inherit the private properties name and age of the parent class
Private properties can be obtained directly through setter and getter methods in subclasses, so the private content should actually provide an exit for operations.
subclass target instantiation process
Let's verify whether the subclass target first calls the no-parameter structure method of the parent class when instantiating it, and then calls the structure method of the subclass. Let's look at Demo04
classPerson
{
privateStringname;
privateintage;
publicPerson()
{
System.out.println(
The structure method of the parent class
);
}
//Private properties can be accessed by participating in setter and getter operations~
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicStringgetInfo()
{
return name:  this.name , age:  this.age;
}
};
//The comparison between the Student class and the Person class extends the function of the Person class, so it can be inherited here
classStudentextendsPerson
{
publicStudent()
{
System.out.println(
Subclass structure method
);
}
//If nothing is written here, it should be at least shared with the content of the Person class
Stringschool;
publicvoidfun()
{
setName(Wang Gan);
setAge(27);
[Zero-based JAVA] JavaSE target-oriented part-13. Target-oriented high-grade (01).doc)

[下载]08443898728.rar




上一篇:零基础学习java
下一篇:[零基础学JAVA]Java SE面向对象部分-14.面向对象高级(02)