如何去调用·private关键字中的东西,封装 题目1)定义一个数组int arr = {11,18,0,2,34,0,1,0,32,0,9}

98 阅读1分钟

package suixn;

public class Book { private String book; private String author; String press; private double price; public Book() {} public Book(String book, String author, String press, double price) { super(); this.book = book; this.author = author; this.press = press; this.price = price; } private String getBook() { return book; } private void setBook(String book) { this.book = book; } public String getAuthor() { return author; }

 public  void setAuthor(String author) {
	this.author = author;
}
public String getPress() {
	return press;
}
public void setPress(String press) {
	this.press = press;
}
public double getPrice() {
	return price;
}
public void setPrice(double price) {
	Book b = new Book();
	b.getPrice();
	if(price >10) {
		System.out.println("你输入的价格有误,请重新输入");
		price=10;
	}else {
		this.price=price;
	}
	
		
		
		
	}
	

public void introduce() {
	System.out.println("作者"+author+"出版社"+press);
}

} 测试

package suixn;

public class TestBook { public static void main(String[] args) { Book book =new Book();

	book.press="中华书店";
	System.out.println(book.getPress());
	
	book.setPrice(6);
	System.out.println(book.getPrice());
	
	book.setAuthor("WSS");
	System.out.println(book.getAuthor());
	
	book.introduce();
	
}

}

public class diyi { public static void main(String[] args) {

      int arr[] = {11,18,0,2,34,0,1,0,32,0,9};
                   arr=sort(arr);
                   for (int i = 0; i < arr.length; i++) {
					System.out.println(arr[i]);
				}
          
			}
	public static int[] sort(int arr[]){
		//对元素组中的0的个数计数
		int count =0;
		for (int i = 0; i < arr.length; i++) {
			if(arr[i]==0) {
				count++;
				
			}
		}
		
		int arr1[]=new int[arr.length-count];
		//定义新数组中的下标
		int index=0;
		//将原数组中的不为0的数赋给新数组
		for (int i = 0; i < arr.length; i++) {
			if(arr[i]!=0) {
				
			arr1[index]=arr[i];
			index++;
			}
			
		}
		
		return arr1;
		
		
		
		
	}
}