package com.heima.array;
public class Test6 {
public static void main(String[] args) {
Student s = new Student("张三",30,"男");
s.print();
}
}
class Person {
private String name;
private int age;
int num = 20;
public Person() {
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String show() {
return "abc";
}
}
class Student extends Person {
private String gender;
int num = 10;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String name, int age,String gender) {
super(name, age);//这里用了super()调用父类的构造方法,只能写在第一样,但是不能在用this()
this.gender = gender;//这里的this是用于区分成员变量和局部变量同名的情况
}
public String show() {
return "def";
}
public void print(){
System.out.println("Person 中的show:"+super.show()+", Student中的show:"+this.show()+", Person中的num:"+super.num+", Student中的num:"+this.num);
}
}
哥们看懂这个就差不多了 |