欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 第14章 :综合案例:继承分析

程序员文章站 2022-05-08 09:20:55
课时67:案例分析一(学生类)补习一下this调用构造函数的例子:public CrawlDatum(){} public CrawlDatum(int status, int fetchInterval) { this(); /*this调用的作用是调用上面的无参构造函数*/ this.status = (byte)status; this.fetchInterval = fetchInterval; } public CrawlDatum(int s...

课时67:案例分析一(学生类)

补习一下this调用构造函数的例子:

public CrawlDatum(){} 
public CrawlDatum(int status, int fetchInterval) { 
    this(); /*this调用的作用是调用上面的无参构造函数*/ 
    this.status = (byte)status; 
    this.fetchInterval = fetchInterval; 
    } 
public CrawlDatum(int status, int fetchInterval, float score) { 
    this(status, fetchInterval); /*this的作用是调用上面有两个参数的构造函数*/
    this.score = score; 
    } 

1, 实现代码复用。例如上面第三个构造函数如果不使用this就会是这样

public CrawlDatum(int status, int fetchInterval, float score) { 
    this.status = (byte)status; 
    this.fetchInterval = fetchInterval; 
    this.score = score; 
  } 

这样的话,前两行代码就完全是第二个构造函数中的语句,违背面向对象代码复用的原则。

2, 封装 如果说第二个构造函数的实现发生了变化,则我们除了修改第二个构造函数,还得修改第三个构造函数。 如果使用this就不需要修改第三个构造函数了。

 

package test;
 class Person {
    private String name;
    private String addr;
    private char sex;
    private int age;
    public Person(){}
    public Person(String name,String addr){
        this(name,addr,'男',0);
    }
    public Person (String name,String addr,char sex,int age){
        this.name = name;
        this.addr = addr;
        this.sex = sex;
        this.age = age;
    }
    public String getInfo(){
        return "姓名:"+ this.name + "、性别:" + this.sex+ "、年龄:"+ this.age +"、地址:"+ this.addr;
    }
}
class Student extends Person{
    private double match;
    private double english;
    public Student(){}  //无参
    public Student(String name , String addr){   //两参
        super(name,addr);
    }
    public Student(String name,String addr,char sex,int age,double match,double english){   //六参
        super(name,addr,sex,age);//重点!!! super应在首行调用
        this.match = match;
        this.english = english;
    }
    public String getInfo(){
        return super.getInfo() + "、数学成绩:"+ this.match + "、英语成绩" + this.english ;
    }
}
public class example {
    public static void main(String[] args) {
        Student stu = new Student("暖光","天府广场",'男',3,99.8,99.9) ;
        System.out.println(stu.getInfo() );
        }
      }
/*
不能被继承的:
          1.私有的属性(可以通过set get或super方法使用父类的私有属性)
          2.不同包的friendly
          3.构造方法不能继承
 */

课时68:案例分析二(管理人员与职员)

package test;
class Employee{
    private String name;
    private int age;
    private String sex;
    public Employee(){}
    public Employee (String name,int age, String sex){
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public String getInfo(){
        return "姓名:"+this.name+"、年龄:"+this.age+"、性别:"+this.sex;
    }
}
class Manager extends Employee{
    private String job;
    private double yearSalary;
    public Manager (){}
    public Manager (String name,int age,String sex,String job,double yearSalary){   //继承父类此处也应该添加成员属性才可继承
        super(name,age,sex);
        this.job = job;
        this.yearSalary = yearSalary;
    }
    public String getInfo(){
        return "【管理层】:"+super.getInfo() + "、职务:"+this.job + "、年薪:"+this.yearSalary ;
    }
}
class Worker extends Employee{
    private String department;
    private double monthSalary;
    public Worker(){}
    public Worker(String name,int age, String sex,String department,double monthSalary ){
        super(name,age,sex);
        this.department = department ;
        this.monthSalary = monthSalary ;
    }
    public String getInfo(){
        return "【职员】:"+super.getInfo() +"、部门:"+this.department +"、月薪:"+this.monthSalary ;
    }
}
public class example {
    public static void main(String[] args) {
         Manager man = new Manager("张三",30,"男","主管",15000.00);
         Worker str = new Worker("李斯",25,"女","出纳",3000.00);
         System.out.println(man.getInfo());
         System.out.println(str.getInfo());
    }
      }

课时69:案例分析三(字符串统计)

统计“want you to know one thing”字母“n”、“o”出现次数。

方案1:心比一个单独的处理类

 

 

本文地址:https://blog.csdn.net/u012820292/article/details/109561011

相关标签: java