继承应用案例分析

案例1


定义员工类,具有姓名、年龄、属性,并且有构造方法和显示数据方法。
定义管理层,拥有自己的属性职务和年薪。
定义职工类,继承员工类,并有自己的属性所属部门和月薪。

class Employee{   //定义员工类
public String name;
public int age;
public Employee(){}
public Employee(String name,int age){
this.name=name;
this.age=age;
    }
public String getInfo(){
return this.name+this.age;
    }
  }
class Leader extends Employee{    //定义管理层继承员工
private String job; //独有属性
private double yearsal;
public Leader(){}
public Leader(String name,int age,String job,double yearsal){
super(name,age);
this.job=job;
this.yearsal=yearsal;
    }
public String getInfo(){
return super.getInfo()+this.job+this.yearsal;
    }
  }
class Worker extends Employee{  //定义职工继承员工
private String deptno; 
private double mouthsal;
public Worker(){}
public Worker(String name,int age,String deptno,double mouthsal){
super(name,age);
this.deptno=deptno;
this.mouthsal=mouthsal;
	}
public String getInfo(){
return super.getInfo()+this.deptno+this.mouthsal;
   }
  }
public class  Lier{   //主类
public static void main(String[] args) 
{
	System.out.println("Hello World!");
	Leader a=new Leader("叶修",16,"嘉实",300000); 
	Worker b=new Worker("叶秋",16,"薇草",3000); 
	System.out.println(a.getInfo());//叶修16嘉实300000.0
	System.out.println(b.getInfo());//叶秋  161233000.0
	}
}

输出:
Hello World!
叶修16嘉实300000.0
叶秋16薇草33000.0

案例2


统计字符串“want you to know one things about our future”中o和n出现的次数
方法一:

class Tongji{
public static int [] count(String str){
    int countData []= new int [2];//统计个数
    char [] data=str.toCharArray();//字符串转为字符数组
	for(int x=0;x< data.length;x++){
	if (data[x] =='n' || data[x]=='N'){
	countData[0] ++;
	}
	if (data[x]=='o' || data[x]=='O'){
	countData[1]++;
    }
  }
  return countData;
    }
}
public class Lisan {
public static void main(String args []) {
	String str="want you to know one things ";
	int result []=Tongji.count(str);
	System.out.println("n个数:"+result[0]);//n个数4
	System.out.println("o个数:"+result[1]);//o个数4
	}
}

输出:
n个数:4
o个数:4

方法二:

class Jishu{
private String str ;//保存字符串
public Jishu(String str){
	this.str=str;
}
public String getStr(){
return this.str;
 }
public String getInfo(){
return this.str;
 }
}
class Shumu extends Jishu{
private int counta;//计数
private int countb;
public Shumu(String str){
super(str);
this.count();//构造方法统计
	}
public void count(){
   //super.getStr()指的是父类字符串
char [] data=super.getStr().toCharArray();//字符串转为字符数组
    for(int x=0;x< data.length;x++){
	if (data[x] =='n' || data[x]=='N'){
	this.counta ++;
	}
	if (data[x]=='o' || data[x]=='O'){
	this.countb ++;
    }
  }
}
public int getCounta(){
  return this.counta;
  }
public int getCountb(){
  return this.countb;
  }
public String getInfo(){
return "字母n个数:"+this.counta+"、字母o个数:"+this.countb;
  }
}
public class Lisan {
public static void main(String args []) {
	Shumu s=new Shumu ("want you to know one things");
		System.out.println(s.getInfo());//字母n个数:4、字母o个数:4
  }
}

输出:字母n个数:4、字母o个数:4


案例3

继承应用案例分析_第1张图片

class Array{
private int a [];//整型数组
private int leng;
private int foot;//通过角标进行数组索引控制
public Array(int leng){
if (leng>0){
  this.a=new int [leng];//开辟数组
  }else {
  this.a=new int [1];//开辟一个空间
	  }
} 
//实现数组容量扩充,给出的是扩充大小,实际大小:已有大小+扩充大小
public void inc(int num){
	int newA []=new int [this.a.length+num];
	System.arraycopy(this.a,0,newA,0,this.a.length);
	this.a=newA;//改变数组引用
}
public boolean add(int num){//数组增加
	if(this.foot

输出:
true
true
true
true
true
true
true
10、11、12、30、100、1、0、

你可能感兴趣的:(7-日常工作总结,继承,应用)