如果现在父类的一个方法定义成private访问权限,在子类中将此方
法声明为default访问权限,那么这样还叫重写吗?
NO
package top.qaqaq.java.P274;
/*
定义一个ManKind类,包括
成员变量int sex和int salary;
方法void manOrWoman():根据sex的值显示“man”(sex==1)或者“woman”(sex==0);
方法void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。
*
*
*/
public class ManKind {
private int sex;// 性别
private int salary;// 薪资
public ManKind() {
}
public ManKind(int sex, int salary) {
this.sex = sex;
this.salary = salary;
}
public void manOrWoman() {
if (sex == 1) {
System.out.println("man");
} else if (sex == 0) {
System.out.println("woman");
}
}
public void employeed() {
// if(salary == 0) {
// System.out.println("no job");
// }else {
// System.out.println("job");
// }
// 或
String jobInfo = (salary == 0) ? "no job" : "job";
System.out.println(jobInfo);
}
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;
}
}
package top.qaqaq.java.P274;
/*
定义类Kids继承ManKind,并包括
成员变量int yearsOld;
方法printAge()打印yearsOld的值。
*
*
*/
public class Kids extends ManKind {
private int yearsOld;
public Kids() {
}
public Kids(int yearsOld) {
this.yearsOld = yearsOld;
}
public void printAge() {
System.out.println("I am " + yearsOld + " years old.");
}
public int getYearsOld() {
return yearsOld;
}
public void setYearsOld(int yearsOld) {
this.yearsOld = yearsOld;
}
/*
* 修改练习1.2中定义的类Kids,在Kids中重新定义employeed()方法,
* 覆盖父类ManKind中定义的employeed()方法,
* 输出“Kids should study and no job.”
*
*/
public void employeed() {
System.out.println("Kids should study and no job.");
}
}
package top.qaqaq.java.P274;
/*
* 定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问其父类的成员变量及方法。
*
*
*/
public class KidsTest {
public static void main(String[] args) {
Kids someKid = new Kids(12);
someKid.printAge();
someKid.setSalary(0);
someKid.setSex(1);
someKid.employeed();
someKid.manOrWoman();
}
}
package top.qaqaq.java.P274;
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 Math.PI * radius * radius;
}
}
package top.qaqaq.java.P274;
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 findVolume() {
return Math.PI * getRadius() * getRadius() * getLength();
// return findArea() * getLength();
}
@Override
public double findArea() {//返回圆柱的表面积
return Math.PI * getRadius() * getRadius() * 2 +
2 * Math.PI * getRadius() * getLength();
}
}
package top.qaqaq.java.P274;
public class CylinderTest {
public static void main(String[] args) {
Cylinder cy = new Cylinder();
cy.setRadius(2.1);
cy.setLength(3.4);
double volume = cy.findVolume();
System.out.println("圆柱的体积为:" + volume);
//没有重写findArea()时;
// double area = cy.findArea();
// System.out.println("底面圆的面积:" + area);
//重写findArea()以后
double area = cy.findArea();
System.out.println("圆柱的表面积:" + area);
}
}