1.特点
java只支持单继承,一个儿子继承一个父亲
但可以多层继承,a继承b,b继承c
b是a的直接父类,c是a的间接父类
每个类都直接或者简介继承Object,不写继承就默认继承它
2.注意事项
构造方法
父类的构造方法,子类不能继承,要再写一遍
比较表面的理解:因为构造方法和父类的名字相同,那你子类的构造方法名字也必须和子类的名字相同,你子类继承父类的构造方法,名字是父类的,名字就不能和子类一样了,所以就不能继承。
package a01static;
public class StudentTest {
public static void main(String[] args) {
zi z = new zi();
z.hobby();
}
}
class fu{
String name="fu";
public fu(){
System.out.println("fu的无参构造");
}
public fu(String name){
this.name=name;
System.out.println("fu的有参构造");
}
public void hobby(){
System.out.println("喝茶");
}
}
class zi extends fu{
String name="zi";
@Override //重写的标记
public void hobby(){
System.out.println("打游戏");
}
}
一开始就打印fu的无参构造,说明一开始会自动super();
也可以在子类的构造方法中super父类的构造方法
package a01static;
public class StudentTest {
public static void main(String[] args) {
zi z = new zi();
z.hobby();
}
}
class fu{
String name="fu";
public fu(){
System.out.println("fu的无参构造");
}
public fu(String name){
this.name=name;
System.out.println("fu的有参构造");
}
public void hobby(){
System.out.println("喝茶");
}
}
class zi extends fu{
public zi(){
super("jack");
System.out.println("使用了zi的无参构造");
}
@Override //重写的标记
public void hobby(){
System.out.println("打游戏");
}
}
也可以自己写子类的有参构造
package a01static;
public class StudentTest {
public static void main(String[] args) {
zi z = new zi("leo");
z.hobby();
}
}
class fu{
String name="fu";
public fu(){
System.out.println("fu的无参构造");
}
public fu(String name){
this.name=name;
System.out.println("fu的有参构造");
}
public void hobby(){
System.out.println("喝茶");
}
}
class zi extends fu{
public zi(){
System.out.println("使用了zi的无参构造");
}
public zi(String name){
System.out.println("使用了zi的有参构造");
this.name=name;
}
@Override //重写的标记
public void hobby(){
System.out.println(this.name+"打游戏");
}
}
成员变量
不管私有非私有的成员变量都可以继承,但是私有的成员变量不能被直接调用,要用其他的方法
比如说getname()方法
一般:就近原则,谁离得近就用谁
先在局部找,再在子类找,然后在父类找
package a01static;
public class StudentTest {
public static void main(String[] args) {
zi z = new zi();
z.zishow();
}
}
class fu{
String name="fu";
}
class zi extends fu{
String name="zi";
public void zishow(){
String name="zishow";
System.out.println(name);
}
}
在此,String name="zishow";比较近,所以是zishow
package a01static;
public class StudentTest {
public static void main(String[] args) {
zi z = new zi();
z.zishow();
}
}
class fu{
String name="fu";
}
class zi extends fu{
String name="zi";
public void zishow(){
//String name="zishow";
System.out.println(name);
}
}
在此,String name="zi";比较近,所以是zi
package a01static;
public class StudentTest {
public static void main(String[] args) {
zi z = new zi();
z.zishow();
}
}
class fu{
String name="fu";
}
class zi extends fu{
//String name="zi";
public void zishow(){
//String name="zishow";
System.out.println(name);
}
}
在此,String name="fu";比较近,所以是fu
package a01static;
public class StudentTest {
public static void main(String[] args) {
zi z = new zi();
z.zishow();
}
}
class fu{
String name="fu";
}
class zi extends fu{
String name="zi";
public void zishow(){
String name="zishow";
//就近原则
System.out.println(name);
//使用this,说明是这个类的
System.out.println(this.name);
//使用super,说明是父类的
System.out.println(super.name);
}
}
成员方法
父类中 非private 非static 非final 是存在虚方法表中,子类可以直接找到,可以直接继承
其他没被继承的,不代表不能用。
方法的重写
当父类的方法不能满足子类时,子类可以将继承父类的方法进行重写
package a01static;
public class StudentTest {
public static void main(String[] args) {
zi z = new zi();
z.hobby();
}
}
class fu{
String name="fu";
public void hobby(){
System.out.println("喝茶");
}
}
class zi extends fu{
String name="zi";
public void zishow(){
String name="zishow";
}
}
重写后
@Override //重写的标记
告诉虚拟机,你在重写方法,而不是重新写一个新的方法
package a01static;
public class StudentTest {
public static void main(String[] args) {
zi z = new zi();
z.hobby();
}
}
class fu{
String name="fu";
public void hobby(){
System.out.println("喝茶");
}
}
class zi extends fu{
String name="zi";
public void zishow(){
String name="zishow";
}
@Override //重写的标记
public void hobby(){
System.out.println("打游戏");
}
}
3.带有继承机构的标准javabean类案例
代码结构
employee.java
package employeepakage;
public class employee {
String number;
String name;
String salary;
public employee(String number, String name, String salary) {
this.number = number;
this.name = name;
this.salary = salary;
}
public employee(){
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public void word(){
System.out.println("正在工作");
}
public void eat(){
System.out.println("在吃饭");
}
@Override
public String toString() {
return "employee{" +
"number='" + number + '\'' +
", name='" + name + '\'' +
", salary='" + salary + '\'' +
'}';
}
}
manager.java
package employeepakage;
public class manager extends employee {
double bouns;
public manager(){
}
public manager(String number, String name, String salary, double bouns) {
super(number, name, salary);
this.bouns = bouns;
}
public double getBouns() {
return bouns;
}
public void setBouns(double bouns) {
this.bouns = bouns;
}
@Override
public void word(){
System.out.println("管理人员");
}
@Override
public String toString() {
return "manager{" +
"bouns=" + bouns +
", number='" + number + '\'' +
", name='" + name + '\'' +
", salary='" + salary + '\'' +
'}';
}
}
cooker.java
package employeepakage;
public class cooker extends employee{
public cooker(){
}
public cooker(String number, String name, String salary) {
super(number, name, salary);
}
@Override
public void word(){
System.out.println("炒菜");
}
@Override
public String toString() {
return "cooker{" +
"number='" + number + '\'' +
", name='" + name + '\'' +
", salary='" + salary + '\'' +
'}';
}
}
Test.java
package employeepakage;
public class Test {
public static void main(String[] args) {
manager m = new manager("001","jack","8000",1000);
m.word();
m.eat();
System.out.println(m.toString());
cooker c=new cooker("002","amy","5000");
c.word();
c.eat();
System.out.println(c.toString());
}
}