第十四天(继承)

本文通过多个示例深入探讨了Java中继承与多态的概念,包括不同类型的构造方法调用、super关键字的使用、方法覆盖及重写等关键知识点,并提供了具体的代码案例。
1. (继承、this super 关键字)有以下代码
class Super{
public Super(){
System.out.println("Super()");
}
public Super(String str){
System.out.println("Super(String)");
}
}
class Sub extends Super{
public Sub(){
System.out.println("Sub()");
}
public Sub(int i){
this();
System.out.println("Sub(int)");
}
public Sub(String str){
super(str);
System.out.println("Sub(String)");
}
}
public class TestSuperSub{
public static void main(String args[]){
Sub s1 = new Sub();
Sub s2 = new Sub(10);
Sub s3 = new Sub("hello");
}
}

写出该程序运行的结果。

输出结果:
Super()
Sub()
Super()
Sub()
Sub(int)
Super(String)
Sub(String)

2. super)看下面代码,写出程序运行的结果
class Super{
public void m1(){
System.out.println("m1() in Super" );
}
public void m2(){
System.out.println("m2() in Super" );
}
}
class Sub extends Super{
public void m1(){
System.out.println("m1() in Sub");
super.m1();
}
}
public class TestSuperSub{
public static void main(String args[]){
Sub s = new Sub();
s.m1();
s.m2();
}
}

输出结果:
m1() in Sub
m1() in Super
m2() in Super

3(继承、对象构造过程)有以下代码
class Meal{
public Meal(){
System.out.println("Meal()");
}
}
class Lunch extends Meal{
public Lunch(){
System.out.println("Lunch()");
}
}
class Vegetable {
public Vegetable(){
System.out.println("Vegetable()");
}
}
class Potato extends Vegetable{
public Potato(){
System.out.println("Potato()");
}
}
class Tomato extends Vegetable{
public Tomato(){
System.out.println("Tomato()");
}
}
class Meat{
public Meat(){
System.out.println("Meat()");
}
}
class Sandwichextends Lunch{
Potato p = new Potato();
Meat m = new Meat();
Tomato t = new Tomato();
public Sandwich(){
System.out.println("Sandwich()");
}
public class TestSandwich{
public static void main(String args[]){
Sandwich s = new Sandwich();
}
}
写出这段代码的输出结果。

输出结果:
Meal()
Lunch()
Vegetable()
Potato()
Meat()
Vegetable()
Tomato()
Sandwich()

4 *(方法覆盖)有如下代码
class Super{
int method(){return 0;}
}
class Sub extends Super{
// 1
}
//1 处,能编译通过的代码为:
A. public int method(){return 0;}
B. void method(){}
C. void method(int n){}
AC

5. *(方法覆盖)有如下代码
class Super{
private void method(){}
}
class Sub extends Super{
//1
}
//1 处,能编译通过的代码为:
A. public int method(){return 0;}
B. void method(){}
C. void method(int n){}
D. private void method(){}

ABCD


6.

package day14;
 
public class Demo14_3 {
        public static void main(String[] args) {
                Student stu=new Student("张三",23,"江苏","214000","1381402222");
                System.out.println(stu.getName());
                System.out.println(stu.getAge());
                System.out.println(stu.getAdress());
                System.out.println(stu.getZipCode());
                System.out.println(stu.getMobile());
                stu.getPostAddress();
        }
}
class Student{
        private String name;
        private int age;
        private String adress;
        private String zipCode;
        private String mobile;
        public Student(String name,int age,String adress,String zipCode,String mobile){
                this.name=name;
                this.age=age;
                this.adress=adress;
                this.zipCode=zipCode;
                this.mobile=mobile;
        }
        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 getAdress() {
                return adress;
        }
        public void setAdress(String adress) {
                this.adress = adress;
        }
        public String getZipCode() {
                return zipCode;
        }
        public void setZipCode(String zipCode) {
                this.zipCode = zipCode;
        }
        public String getMobile() {
                return mobile;
        }
        public void setMobile(String mobile) {
                this.mobile = mobile;
        }
        public void getPostAddress(){
                System.out.println(adress+" "+zipCode);
        }
}


7.

class Account{
        private int id;
        private double balance;
        private String password;
        public int getId() {
                return id;
        }
        public void setId(int id) {
                this.id = id;
        }
        public double getBalance() {
                return balance;
        }
        public void setBalance(double balance) {
                this.balance = balance;
        }
        public String getPassword() {
                return null;
        }
        public void setPassword(String password) {
                if(password.length()!=6){
                        return;
                }
                this.password = password;
        }
}
 
class SaveAccount extends Account{
        private double interestRate;
 
        public double getInterestRate() {
                return interestRate;
        }
 
        public void setInterestRate(double interestRate) {
                if(interestRate>0 && interestRate<10/100){
                        this.interestRate = interestRate;
                }
        }
}
 
class CreditAccount extends Account{
        private double creditLine;
}

8、描述不同的动物不同的叫法

        1:定义动物类

                有名字,有吃和叫的方法

        2:定义狗继承动物重写父类吃和叫的方法

        3:定义猫继承动物重写父类吃和叫的方法

   要求:通过主函数调用相关函数和属性信息信息
class Animal {
        private String name;
 
        public String getName() {
                return name;
        }
 
        public void setName(String name) {
                this.name = name;
        }
         
        public void eat(){
                System.out.println(name+"吃東西");
        }
         
        public void call(){
                System.out.println(name+"在叫...");
        }
}
 
class Dog extends Animal{
        @Override
        public void eat() {
                System.out.println(super.getName()+"在吃東西");
        }
         
        @Override
        public void call() {
                System.out.println(super.getName()+"在叫....");
        }
}
 
class Cat extends Animal{
        @Override
        public void eat() {
                System.out.println(super.getName()+"在吃東西");
        }
         
        @Override
        public void call() {
                System.out.println(super.getName()+"在叫....");
        }
}
 
public class TestAnimal{
        public static void main(String[] args) {
                Animal a1 = new Dog();
                a1.setName("夠");
                a1.eat();
                a1.call();
                System.out.println();
                Animal a2 = new Dog();
                a2.setName("貓");
                a2.eat();
                a2.call();
        }
}

9、某公司的雇员分为以下若干类:

   Employee:这是所有员工总的父类,

   属性:员工的姓名和生日月份。

   方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。

   SalariedEmployeeEmployee的子类,拿固定工资的员工。

   属性:月薪

   HourlyEmployeeEmployee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放

   属性:每小时的工资、每月工作的小时数

   SalesEmployeeEmployee的子类,销售人员,工资由月销售额和提成率决定

   属性:月销售额、提成率

   BasePlusSalesEmployeeSalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分  

   属性:底薪。

   实现:1、李易峰 固定员工 ,月薪3000,生日月份2月,工作180小时,获取工资多少

         2、张子明 小时工,每小时25,工作180小时,获取工资多少

         3、林肯  销售人员 月销售额30万,提成0.3% 获取工资

         4、阳光  固定底薪销售人员 底薪1500 ,销售20万,提成0.2% 获取工资

public class TestEmployee {
 
        public static void main(String[] args) {
                SalariedEmployee se = new SalariedEmployee();
                se.setName("李易峰");
                se.setSalary(3000);
                se.setBirthMonth(2);
                se.getSalary(2);
 
                HourlyEmployee he = new HourlyEmployee();
                he.setName("张子明");
                he.setHourSalary(25);
                he.setHours(180);
                he.getSalary();
 
                SalesEmployee sae = new SalesEmployee();
                sae.setName("林肯");
                sae.setMonthsales(300000);
                sae.setRoyalRate(0.003);
                sae.getSalary();
 
                BasePlusSalesEmployee bse = new BasePlusSalesEmployee();
                bse.setName("阳光");
                bse.setBaseSalary(1500);
                bse.setMonthsales(200000);
                bse.setRoyalRate(0.002);
                bse.getSalary();
        }
}
 
class Employee {
        String name;
        int birthMonth;
        double salary;
 
        public String getName() {
                return name;
        }
 
        public void setName(String name) {
                this.name = name;
        }
 
        public int getBirthMonth() {
                return birthMonth;
        }
 
        public void setBirthMonth(int birthMonth) {
                this.birthMonth = birthMonth;
        }
 
        public void setSalary(double salary) {
                this.salary = salary;
        }
 
        public void getSalary(int month) {
                if (birthMonth == month) {
                        System.out.println(name + "当月月薪" + (salary + 100));
                }
        }
}
 
class SalariedEmployee extends Employee {
 
}
 
class HourlyEmployee extends Employee {
        double hourSalary;
        double hours;
        double salary;
 
        public double getHourSalary() {
                return hourSalary;
        }
 
        public void setHourSalary(double hourSalary) {
                this.hourSalary = hourSalary;
        }
 
        public double getHours() {
                return hours;
        }
 
        public void setHours(double hours) {
                this.hours = hours;
        }
 
        public void setSalary(double salary) {
                this.salary = salary;
        }
 
        public void getSalary() {
                if (hours > 160) {
                        System.out.println(name + "月薪为" + (hourSalary * 160 + ((hours - 160) * hourSalary * 1.5)));
                } else {
                        System.out.println(name + "月薪为" + hourSalary * hours);
                }
 
        }
}
 
class SalesEmployee extends Employee {
        double monthsales;
        double royalRate;
 
        public double getMonthsales() {
                return monthsales;
        }
 
        public void setMonthsales(double monthsales) {
                this.monthsales = monthsales;
        }
 
        public double getRoyalRate() {
                return royalRate;
        }
 
        public void setRoyalRate(double royalRate) {
                this.royalRate = royalRate;
 
        }
 
        public void setSalary(double salary) {
                this.salary = salary;
        }
 
        public void getSalary() {
                System.out.println(name + "月薪为" + monthsales * royalRate);
        }
 
}
 
class BasePlusSalesEmployee extends SalesEmployee {
        double baseSalary;
 
        public double getBaseSalary() {
                return baseSalary;
        }
 
        public void setBaseSalary(double baseSalary) {
                this.baseSalary = baseSalary;
        }
 
        public void getSalary() {
                System.out.println(name + "月薪为" + baseSalary + monthsales * royalRate);
        }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值