Java编程思想第四版 *第五章 个人练习
练习3:(1)创建一个带默认构造器(即无參构造器)的类。在构造器中打印一条消息。为这个类创建一个对象。P116
public class Test{
	public Test(){
		System.out.println("Hello Word");
	}
	public static void main(String[] args) {
		new Test();
	}
	/**
	 * 执行结果
	 Hello Word
	 */
}
练习4:(1)为前一个练习中的类加入一个重载构造器,令其接受一个字符參数。并在构造器中把你自己的信息和接受的參数一起打印出来。
public class Test{
	public Test(String msg){
		System.out.println("Hello "+msg);
	}
	public static void main(String[] args) {
		new Test("China");
	}
	/**
	 * 执行结果
	 Hello China
	 */
}
练习5:(2)创建一个名为Dog的类,它具有重载的bark()方法。此方法应依据不同的基本数据类型进行重载。并依据被调用的版本号。打印出不同类型的狗吠(barking)、咆哮(howling)等信息,编写main()来调用全部不同版本号的方法。
public class Dog{
	public void bark(){
		System.out.println("barking");
	}
	public void bark(String name){
		System.out.println(name + " howing");
	}
	public static void main(String[] args) {
		Dog dog=new Dog();
		dog.bark();
		dog.bark("Lily");
	}
	/**
	 * 执行结果
	barking
	Lily howing
	 */
}
练习6:(1)改动前一个练习的程序,让两个重载方法各自接受两个类型的不同的參数,但二者顺序相反。验证其是否工作。
public class Dog{
	public void bark(int i){
		System.out.println(i+"barking");
	}
	public void bark(String name){
		System.out.println(name + " howing");
	}
	public static void main(String[] args) {
		Dog dog=new Dog();
		dog.bark("Lily");
		dog.bark(88);
	}
	/**
	 * 执行结果
	Lily howing
	88barking
	 */
}
练习7:(1)创建一个没有构造器的类。并在main()中创建其对象,用以验证编译器是否真的自己主动增加了默认构造器。
public class Dog{
	public void bark(int i){
		System.out.println(i+"barking");
	}
	public void bark(String name){
		System.out.println(name + " howing");
	}
	public static void main(String[] args) {
		Dog dog=new Dog();
		dog.bark("Lily");
		dog.bark(88);
	}
	/**
	 * 执行结果
	Lily howing
	88barking
	 */
}
练习8:(1)编写具有两个方法的类,在第一个方法内调用第二个方法两次:第一次调用时不使用thiskeyword。第二次调用时使用thiskeyword-- * 这里仅仅是为了验证它是起作用的,你不应该在实践中使用这样的方法。
public class Test{	
	public static void main(String[] args) {
		Apple apple=new Apple();
		apple.getApple();
	}
	/**
	 * 执行结果
		未使用this调用:已创建好……
		使用this调用:已创建好……
	 */
}
class Apple{
	public void getApple(){
		System.out.print("未使用this调用:");
		createApple();
		System.out.print("使用this调用:");
		this.createApple();
	}
	public void createApple(){
		System.out.println("已创建好……");
	}
}
练习9:(1)编写两个(重载)构造器的类,并在第一个构造器中,通过this调用第二个构造器。
package mil.oms.main.test;
public class Test{	
	public static void main(String[] args) {
		new Apple();
	}
	/**
	 * 执行结果
		颜色:red
	 */
}
class Apple{
	public Apple(){
		this("red");
	}
	public Apple(String color){
		System.out.println("颜色:"+color);
	}
}
练习10:(2)编写具有finalize()方法的类,并在方法中打印消息。在main()中为该类创建一个对象,试解释这个程序的行为。
public class Test{	
	public static void main(String[] args) {
		Apple apple=new Apple();
		apple=null;
		System.gc();
	}
	/**
	 * 执行结果
		<span style="font-family: Arial, Helvetica, sans-serif;">----finalize---------</span>
	 */
}
class Apple{
	protected void finalize(){
		System.out.println("----finalize---------");
	}
}
练习11:(4)改动前一个练习的程序,让你的finalize()总会被调用。
public class Test{	
	public static void main(String[] args) {
		new Apple();
		System.gc();
		System.runFinalization();
	}
	/**
	 * 执行结果
		颜色:red
	 */
}
class Apple{
	protected void finalize(){
		System.out.println("----finalize---------");
	}
}
练习12:(4)编写名为Tank的类。此类的状态能够是“满的”或“空的”。
其终结条件是:对象被清理时必须处于空状态。 * 请编写finalize()以检验终结条件是否成立。在main()中測试Tank可能发生的几种使用方式
public class Tank{	
	public boolean state=false;
	public void changeState(boolean state){
		this.state=state;
	}
	protected void finalize() throws Throwable{
if(state){
System.out.println("----满的,有问题---------");
throw new Throwable ("状态不为空!");
}else{
System.out.println("----空的,没问题---------");
super.finalize();
}
}
	public static void main(String[] args) {
		new Tank();
		System.gc();
		System.runFinalization();
		new Tank().changeState(true);
		System.gc();
		System.runFinalization();
	}
	/**
	 * 执行结果
		----空的,没问题---------
		----满的,有问题---------
	 */
}
练习13:(1)验证前面段落中的语句
public class ExpliciStatic{	
	public static void main(String[] args) {
		System.out.println("Inside main()");
		Cups.cup1.f1(1);
	}
	static Cups cups1=new Cups();
	static Cups cups2=new Cups();
	/**
	 * 执行结果
		Bowl(1)
		Bowl(2)
		Cups()
		Cups()
		Inside main()
		f1(1)
	 */
}
class Cup{
	Cup(int mark){
		System.out.println("Bowl("+mark+")");
	}
	void f1(int mark){
		System.out.println("f1("+mark+")");
	}
}
class Cups{
	static Cup cup1;
	static Cup cup2;
	static{
		cup1=new Cup(1);
		cup2=new Cup(2);
	}
	Cups(){
		System.out.println("Cups()");
	}
}
练习14:(1)编写一个类。拥有两个静态字符串域。当中一个在定义处初始化,还有一个在静态块中初始化。如今,增加一个静态方法用以打印出两个字段值。
请证明它们都会在被使用之前完毕初始化动作。
public class ExpliciStatic{	
	public static void main(String[] args) {
		Cup.print();
	}
	/**
	 * 执行结果
		str1:str1
		str2:str2
	 */
}
class Cup{
	static String str1="str1";
	static String str2;
	static{
		str2="str2";
	}
	static void print(){
		System.out.println("str1:"+str1);
		System.out.println("str2:"+str2);
	}
}
练习15:(1)编写一个含有字符串域的类,并採用实例初始化方式进行初始化。
public class ExpliciStatic{	
	public static void main(String[] args) {
		System.out.println("start()");
		new Cup();
		System.out.println("end()");
	}
	/**
	 * 执行结果
		start()
		字段串域初始化完毕
		Cup()
		end()
	 */
}
class Cup{
	String str;
	{
		str="Hello";
		System.out.println("字段串域初始化完毕");
	}
	Cup(){
		System.out.println("Cup()");
	}
}
练习16创建一个String对象数据,并为每个元素都赋值一个String。用for循环来打印该数组。
public class ExpliciStatic{	
	public static void main(String[] args) {
		String[] strs={"H","e","l","l","o"};
		for(String str:strs){
			System.out.print(str);
		}
	}
	/**
	 * 执行结果
		Hello
	 */
}
练习17:创建一个类,他有一个接受一个String參数的构造器。
在构造阶段,打印该參数。创建一个该类的对象引用数组。可是不实际去创建对象赋值给该数组。
当执行程序时。请注意来自对该构造器的调用中的初始化消息是否打印了出
public class ExpliciStatic{	
	public static void main(String[] args) {
		Random random=new Random(47);
		Cup[] cups=new Cup[random.nextInt(10)];
	}
	/**
	 * 执行结果
	 */
}
class Cup{
	Cup(String str){
		System.out.println("Cup("+str+")");
	}
}
练习18:通过创建对象赋值给引用数组,从而完毕前一个练习。
public class ExpliciStatic{	
	public static void main(String[] args) {
		Random random=new Random(47);
		Cup[] cups=new Cup[random.nextInt(10)];
		for(int i=0,j=cups.length;i<j;i++){
			cups[i]=new Cup("cup"+i);
		}
	}
	/**
	 * 执行结果
		Cup(cup0)
		Cup(cup1)
		Cup(cup2)
		Cup(cup3)
		Cup(cup4)
		Cup(cup5)
		Cup(cup6)
		Cup(cup7)
	 */
}
class Cup{
	Cup(String str){
		System.out.println("Cup("+str+")");
	}
}
练习19:(1)写一个类,他接受一个可变參数的String数组,验证你能够向该方法传递一个用逗号分隔的String列表。或是一个String[]。
<pre name="code" class="html">public class ExpliciStatic{	
	public static void main(String[] args) {
		String str="Hello",strs[]={str};
		System.out.print("传 入 字 符 串 :");
		new Cup(str);
		System.out.print("传入字符串数组:");
		new Cup(strs);
		System.out.print("传入两个字符串:");
		new Cup(str,str);
	}
	/**
	 * 执行结果:
		传 入 字 符 串 :可变。。。參数
		传入字符串数组:可变。。。
參数
		传入两个字符串:可变。。
。參数
	 */
}
class Cup{
	Cup(String ... str){
		System.out.println("可变。。。參数");
	}
}
练习20:(1)创建一个使用可变參数列表而不是普通的main()语法的main()。
打印所产生的args数组的全部元素,并用各种不同数量的命令行參数来測试它。
public class ExpliciStatic{
	public static void main (String... args){
        printStr("H","e","l","l","o");
        printStr(
            new String[] {"H","e","l","l","o",}/** 最后的逗号可有可无 **/
        );
    }
	/**
	 * 执行结果:
		Hello
		Hello
	 */
	public static void printStr(String ... strs){
		for(String str:strs){
			System.out.print(str);
		}
		System.out.println();
	}
}
练习21:创建一个enum,它包括纸币中最小面值的6种类型。通过values()循环并打印每个值及其ordinal()
public class ExpliciStatic{
	public static void main (String args[]){
		for(Money m:Money.values()){
			System.out.println(m+"\t "+m.ordinal());
		}
    }
	/**
	 * 执行结果:
		one	 0
		two	 1
		five 2
		ten	 3
		hun	 4
		tho	 5
	 */
}
enum Money{
	one,two,five,ten,hun,tho
}
练习22:(2)在前面的样例中,为enum写一个switch语句,对于每个case,输出该特定货币的描写叙述。
public class ExpliciStatic{
	public static void main (String args[]){
		for(Money m:Money.values()){
			System.out.println(m+"\t "+m.ordinal());
			Money.describe(m);
		}
    }
	/**
	 * 执行结果:
		one	 0
		1分钱
		two	 1
		2分钱
		five	 2
		5分钱
		ten	 3
		10块钱
		hun	 4
		20块钱
		tho	 5
		50块钱
	 */
}
enum Money{
	one,two,five,ten,hun,tho;
	static void describe(Money m){
		switch(m){
		case one:
			System.out.println("1分钱");break;
		case two:
			System.out.println("2分钱");break;
		case five:
			System.out.println("5分钱");break;
		case ten:
			System.out.println("10块钱");break;
		case hun:
			System.out.println("20块钱");break;
		case tho:
			System.out.println("50块钱");break;
			default:break;
		}
	}
}
Java编程思想第四版 *第五章 个人练习的更多相关文章
- java编程思想第四版第五章习题
		创建一个类, 它包含一个未初始化的String引用.验证该引用被Java初始化成了null package net.mindview.initialization; public class Test ... 
- java编程思想第四版第五章总结
		1. 构造器 构造器的一个重要的作用: 保证对象被使用之前初始化了. 构造器是一种特殊类型的方法, 因为他没有返回值.这与返回值为空(void)明显不同.对于空返回值,尽管方法本身不会自动返回什么, ... 
- java编程思想第四版第十三章字符串  习题
		fas 第二题 package net.mindview.strings; import java.util.ArrayList; import java.util.List; /** * 无限循环 ... 
- java编程思想第四版第十一章习题
		第一题 package net.mindview.holding.test1; import java.util.ArrayList; import java.util.List; /** * 沙鼠 ... 
- Java编程思想第四版*第七章*个人练习
		欢迎加群:239063848 成团的笔记:该组仅用于技术共享和交流,问题和答案公布 潘基聊天.禁止广告.禁止招聘-- 练习1:(2)创建一个简单的类.第二个类中,将一个引用定义为第一个类的对象.运用惰 ... 
- java编程思想第四版第六章习题
		(略) (略) 创建两个包:debug和debugoff,他们都包含一个相同的类,该类有一个debug()方法,第一个版本显示发送给控制台的String参数,而第二版本什么也不做,使用静态import ... 
- java编程思想第四版第六章总结
		1. 代码重构 为什么f要代码重构 第一次代码不一定是完美的, 总会发现更优雅的写法. 代码重构需要考虑的问题 类库的修改不会破坏客户端程序员的代码. 源程序方便扩展和优化 2. 包 创建一个独一无二 ... 
- java编程思想 第四版 第六章 个人练习
		欢迎加群:239063848 进群须知:本群仅用于技术分享与交流.问题公布与解答 禁止闲聊.非诚勿扰 练习1:(1)在某个包中创建一个类,在这个类所处的包的外部创建该类的一个实例. import mi ... 
- java编程思想第四版第十三章字符串  总结
		1. String和StringBulider的使用 通过书中介绍, 我们得知如下结论: 当使用+连接符将字符串进行拼接的时候, 编译器会进行自动优化为使用StringBuilder连接字符串. 当在 ... 
随机推荐
- iTOP-4412开发板使用
			使用环境:win7 旗舰64位,VMware11 使用使用板上提供的ubuntu12.04,用VMWARE直接打开虚拟机,因为之前开发epc9600开发板,所以虚拟机网络已经设置过,加载ubuntu1 ... 
- BZOJ5137: [Usaco2017 Dec]Standing Out from the Herd(广义后缀自动机,Parent树)
			Description Just like humans, cows often appreciate feeling they are unique in some way. Since Farme ... 
- 一、Docker安装
			原文:一.Docker安装 如果没有特殊要求关闭selinux!关闭selinux!关闭selinux!重要事情说三遍.这个坑活活让我重装了3.4遍系统才发现问题 本系列基于Centos系统安装,包括 ... 
- 【Codeforces Round #455 (Div. 2) B】Segments
			[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出所有的线 其实就是区间. 总共有n*(n+1)/2个 然后按照左端点.右端点排序 每次取最左边的线. 多种可能就取右端点尽量小 ... 
- Linux下vi替换字符命令操作实例
			在Linux下的开发中,经常涉及到对文件里的字符进行处理,当中,对字符的替换操作也是非常的频繁. 本文以一个实际的文件为例,具体介绍了Linux下经常使用的vi替换字符命令,为相关的开发工作提供给了參 ... 
- php数组函数(分类基本数组函数,栈函数,队列)
			php数组函数(分类基本数组函数,栈函数,队列函数) 一.总结 1.常用数组函数 函数 描述 array() 创建数组. array_combine() 通过合并两个数组来创建一个新数组. array ... 
- 26.SpringBoot事务注解详解
			转自:https://www.cnblogs.com/kesimin/p/9546225.html @Transactional spring 事务注解 1.简单开启事务管理 @EnableTrans ... 
- 1.1 Introduction中 Kafka as a Storage System官网剖析(博主推荐)
			不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Storage System kafka作为一个存储系统 An ... 
- 微软重生:4年市值U型大逆转,超越谷歌重返巅峰!
			划重点: 智东西(公众号:zhidxcom)文 | 寓扬 在最近的两个星期里,微软和谷歌正在进行一场市值大比拼,双方在7700亿美元上下厮杀正紧,抢夺着全球市值第三大公司的宝座(前两位为市值超过900 ... 
- 微服务实战(二):使用API Gateway - DockOne.io
			原文:微服务实战(二):使用API Gateway - DockOne.io [编者的话]本系列的第一篇介绍了微服务架构模式.它讨论了采用微服务的优点和缺点,除了一些复杂的微服务,这种模式还是复杂应用 ... 
 
			
		