一:子类获取父类声明的所有的属性和方法
package com.thisTest.java;
public class Person0 {
String name;
int age;
public Person0() {
}
public Person0(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println("吃饭");
}
public void play() {
System.out.println("玩");
}
}
package com.thisTest.java;
public class Student extends Person0 {
String major;
public Student() {
}
public Student(String name, int age,String major) {
this.name = name;
this.age = age;
this.major = major;
}
public void study() {
System.out.println("学习");
}
}
package com.thisTest.java;
public class StudentTest {
public static void main(String[] args) {
Person0 p = new Person0("张三",18);
p.eat();
Student s = new Student("王五",19,"English");
s.eat();
s.study();
}
}
二、 如果将person中个别属性用private封装起来
发现Student中无法使用age了,所以这里可能两种情况:一是没继承到,二是继承了但无法访问。(studentTest中不必多说,该类中不涉及继承,就按照之前所学的private外部不能访问理解)
再看这里
package com.thisTest.java;
public class Person0 {
String name;
private int age;
public Person0() {
}
public Person0(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void eat() {
System.out.println("吃饭");
}
public void play() {
System.out.println("玩");
}
}
package com.thisTest.java;
public class Student extends Person0 {
String major;
public Student() {
}
public Student(String name, int age,String major) {
this.name = name;
//this.age = age;
setAge(age);
this.major = major;
}
public void print() {
System.out.println(getAge());
}
public void study() {
System.out.println("学习");
}
}
setAge(age); 可以使用,说明继承到了,只是需要方法才能访问到(如果仅仅继承了setAge方法,没有继承属性,那么方法里的this.age就不能使用)
以上是属性私有,同理方法私有
修改后
package com.thisTest.java;
public class Person0 {
String name;
private int age;
public Person0() {
}
public Person0(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void eat() {
System.out.println("吃饭");
play();
}
private void play() {
System.out.println("玩");
}
}
可以通过调用别的方法来调用私有方法
关于继承性的一些规定
练习
package com.extend.java;
public class Mankind {
private int sex;
private int salary;
public Mankind() {
}
public Mankind(int sex, int salary) {
this.sex = sex;
this.salary = salary;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public void manOrWoman() {
if(sex == 1) {
System.out.println("man");
}else if(sex == 0) {
System.out.println("woman");
}else {
System.out.println("输入错误");
}
}
public void employeed() {
if(salary == 0) {
System.out.println("no job");
}else {
System.out.println("job");
}
}
}
package com.extend.java;
public class Kids extends Mankind {
private int yearsOld;
public Kids() {
}
public Kids(int yearsOld) {
this.yearsOld = yearsOld;
}
public int getYearsOld() {
return yearsOld;
}
public void setYearsOld(int yearsOld) {
this.yearsOld = yearsOld;
}
public void printAge() {
System.out.println(yearsOld);
}
}
package com.extend.java;
public class KidsTest {
public static void main(String[] args) {
Kids kid = new Kids(12);
kid.printAge();
kid.setSalary(500);
kid.setSex(0);
kid.employeed();
kid.manOrWoman();
}
}
package com.extend.java;
public class Circle {
private double radius;
public Circle() {
radius = 1.0;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea() {
return radius*radius*Math.PI;
}
}
package com.extend.java;
public class Cylinder extends Circle{
private double length;
public Cylinder() {
length = 1.0;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double findVolumn() {
return findArea()*length;
}
}
package com.extend.java;
public class CylinderTest {
public static void main(String[] args) {
Cylinder cy = new Cylinder();
double v = cy.findVolumn();
System.out.println("圆柱体积为" + v);
}
}