Comprehensive case: inheritance analysis (2)

Case study three (string statistics)

Write a program to count the number of occurrences of the letter n and the letter o in the string "want you to know one thing".
For this program, the simplest operation method is to define an operation in the main method, or directly define a new class for processing.
Example: Define a separate processing class

class StringUtil{
    
    
	//返回的第一个内容为字母n的个数,第二个内容为字母o的个数
	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 Demo1 {
    
    
	public static void main(String[] args) {
    
    
		String str="want you to know one thing";
		int result[]=StringUtil.count(str);
		System.out.println("字母n的个数:"+result[0]);
		System.out.println("字母o的个数:"+result[1]);
	}
}

Strictly speaking, the above solution is only a sequential thinking mode. Assuming that the number of letters o or n is counted now, it is possible to count other designs in the future.

class StringUtil{
    
    
	private String content;//需要保护字符串
	public StringUtil(String content) {
    
    
		this.content=content;
	}
	public String getContent() {
    
    
		return this.content;
	}
	public String getInfo() {
    
    	//默认的信息返回
		return this.getContent();
	}
}
class StringCount extends StringUtil{
    
    
	private int ncount;
	private int ocount;
	public StringCount(String content) {
    
    
		super(content);
		this.countChar();//构造方法统计
	}
	public void countChar() {
    
    
		char []data=super.getContent().toCharArray();//将字符串变为字符数组
		for (int x = 0; x < data.length; x++) {
    
    
			if (data[x]=='n'||data[x]=='N') {
    
    
				this.ncount++;
			}
			if (data[x]=='o'||data[x]=='O') {
    
    
				this.ocount++;
			}
		}
	}
	public int getNcount() {
    
    
		return this.ncount;
	}
	public int getOcount() {
    
    
		return this.ocount;
	}
	public String getInfo() {
    
    
		return "字母n的个数"+this.ncount+"字母o的个数"+this.ncount;
	}
}
public class Demo1 {
    
    
	public static void main(String[] args) {
    
    
		StringCount sc=new StringCount("want you to know one thing");
		System.out.println(sc.getInfo());
	}
}

Any scheme is ok, if the first scheme is adopted, it is more intuitive, but the second scheme is suitable for structured design.

Case study four (array operation)

Establish an operation class (Array) that can implement an integer array, and then the size of the array that can be manipulated in it is determined by the outside, and then the following processing of the array needs to be provided in the Array class: increase the array (if the data is full It cannot be increased), the capacity of the array can be expanded, and all the contents of the array can be obtained.
After completion, two sub-categories will be created on this basis:

  1. Array sorting class: the returned data must be the sorted result;
  2. Array reversal class: can realize the exchange of content.
    For this program, we must first consider the perfection of the parent class.
    Step 1: Implement the basic array class definition
class Array{
    
    //数组的操作类
	private int []data;//数组类型
	private int foot;//进行数组索引控制
	public Array(int len) {
    
    
		if (len>0) {
    
    
			this.data=new int [len];//开辟数组
		}else {
    
    
			this.data=new int [1];//开辟一个空间
		}
	}
	//实现数组的容量扩充,给出的是扩充大小,实际大小=已有大小+扩充大小
	public void increment(int num) {
    
    
		int newData[]=new int [this.data.length+num];
		System.arraycopy(this.data, 0, newData,0, data.length);
		this.data=newData;//改变数组引用
	}
	public boolean add(int num) {
    
    //数组增加
		if (this.foot<this.data.length) {
    
    //有位置
			this.data[this.foot++]=num;
			return true;
		}
		return false;
	}
	public int []getData(){
    
    
		return this.data;
	}
}
public class Demo1 {
    
    
	public static void main(String[] args) {
    
    
		Array arr=new Array(5);
		System.out.println(arr.add(10));
		System.out.println(arr.add(5));
		System.out.println(arr.add(20));
		System.out.println(arr.add(3));
		System.out.println(arr.add(6));
		arr.increment(3);
		System.out.println(arr.add(1));
		System.out.println(arr.add(7));
		System.out.println(arr.add(0));
	}
}

Step 2: Define the sorting subclass

class Array{
    
    //数组的操作类
	private int []data;//数组类型
	private int foot;//进行数组索引控制
	public Array(int len) {
    
    
		if (len>0) {
    
    
			this.data=new int [len];//开辟数组
		}else {
    
    
			this.data=new int [1];//开辟一个空间
		}
	}
	//实现数组的容量扩充,给出的是扩充大小,实际大小=已有大小+扩充大小
	public void increment(int num) {
    
    
		int newData[]=new int [this.data.length+num];
		System.arraycopy(this.data, 0, newData,0, data.length);
		this.data=newData;//改变数组引用
	}
	public boolean add(int num) {
    
    //数组增加
		if (this.foot<this.data.length) {
    
    //有位置
			this.data[this.foot++]=num;
			return true;
		}
		return false;
	}
	public int []getData(){
    
    
		return this.data;
	}
}
class sortArray extends Array{
    
    	//定义排序子类
	public sortArray(int len) {
    
    
		super(len);
	}
	public int[] getData() {
    
    //获得排序结果
		java.util.Arrays.sort(super.getData());//排序
		return super.getData();
	}
}
public class Demo1 {
    
    
	public static void main(String[] args) {
    
    
		sortArray arr=new sortArray(5);
		System.out.println(arr.add(10));
		System.out.println(arr.add(5));
		System.out.println(arr.add(20));
		System.out.println(arr.add(3));
		System.out.println(arr.add(6));
		arr.increment(3);
		System.out.println(arr.add(1));
		System.out.println(arr.add(7));
		System.out.println(arr.add(0));
		int result[]=arr.getData();
		for(int temp:result) {
    
    
			System.out.print(temp+"、");
		}
	}
}

Step 3: Define the inversion subclass

class Array{
    
    //数组的操作类
	private int []data;//数组类型
	private int foot;//进行数组索引控制
	public Array(int len) {
    
    
		if (len>0) {
    
    
			this.data=new int [len];//开辟数组
		}else {
    
    
			this.data=new int [1];//开辟一个空间
		}
	}
	//实现数组的容量扩充,给出的是扩充大小,实际大小=已有大小+扩充大小
	public void increment(int num) {
    
    
		int newData[]=new int [this.data.length+num];
		System.arraycopy(this.data, 0, newData,0, data.length);
		this.data=newData;//改变数组引用
	}
	public boolean add(int num) {
    
    //数组增加
		if (this.foot<this.data.length) {
    
    //有位置
			this.data[this.foot++]=num;
			return true;
		}
		return false;
	}
	public int []getData(){
    
    
		return this.data;
	}
}
class sortArray extends Array{
    
    	//定义排序子类
	public sortArray(int len) {
    
    
		super(len);
	}
	public int[] getData() {
    
    //获得排序结果
		java.util.Arrays.sort(super.getData());//排序
		return super.getData();
	}
}
class ReverseArray extends Array{
    
    	//定义排序子类
	public ReverseArray(int len) {
    
    
		super(len);
	}
	public int[] getData() {
    
    //获得排序结果
		int center=super.getData().length/2;
		int head=0;
		int tail=super.getData().length-1;
		for (int x = 0; x < center; x++) {
    
    
			int temp=super.getData()[head];
			super.getData()[head]=super.getData()[tail];
			super.getData()[tail]=temp;
			head++;
			tail--;
		}
		return super.getData();
	}
}
public class Demo1 {
    
    
	public static void main(String[] args) {
    
    
		ReverseArray arr=new ReverseArray(5);
		System.out.println(arr.add(10));
		System.out.println(arr.add(5));
		System.out.println(arr.add(20));
		System.out.println(arr.add(3));
		System.out.println(arr.add(6));
		arr.increment(3);
		System.out.println(arr.add(1));
		System.out.println(arr.add(7));
		System.out.println(arr.add(0));
		int result[]=arr.getData();
		for(int temp:result) {
    
    
			System.out.print(temp+"、");
		}
	}
}

The method names defined in the parent class are often very important. If the functions are the same, the subclass should override the method of the parent class as priority

Guess you like

Origin blog.csdn.net/weixin_46688667/article/details/108363102