Wolf class FieldAndMethodTest

薛扬
2023-12-01

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
class Animal
{
	//desc实例变量保存对象toString方法的返回值
	private String desc;
	public Animal()
	{
		//调用getDesc()方法初始化desc实例变量
		this.desc = getDesc();
	}
	public String getDesc()
	{
		return "Animal";
	}
	public String toString()
	{
		return desc;
	}
}
public class Wolf extends Animal
{
	//定义name、weight两个实例变量
	private String name;
	private double weight;
	public Wolf(String name , double weight)
	{
		//为name、weight两个实例变量赋值
		this.name = name;
		this.weight = weight;
	}
	//重写父类的getDesc()方法
	@Override
	public String getDesc()
	{
		return "Wolf[name=" + name + " , weight="
			+ weight + "]";
	}
	public static void main(String[] args)
	{
		System.out.println(new Wolf("灰太郎" , 32.3));
	}
}
/*
Wolf[name=null , weight=0.0]
请按任意键继续. . .
*/

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */

class Base
{
	int count = 2;
	public void display()
	{
		System.out.println(this.count);
	}
}
class Derived extends Base
{
	int count = 20;
	@Override
	public void display()
	{
		System.out.println(this.count);
	}
}
public class FieldAndMethodTest
{
	public static void main(String[] args) 
	{
//		//声明、创建一个Base对象
 		Base b = new Base();
//		//直接访问count实例变量和通过display访问count实例变量
 		System.out.println(b.count);//2
 		b.display();//2
		//声明、并创建一个Derived对象
		Derived d = new Derived();
		//直接访问count实例变量和通过display访问count实例变量
		System.out.println(d.count);//20
		d.display();//20
//		//声明一个Base变量,并将Derived对象赋给该变量
		Base bd = new Derived();
//		//直接访问count实例变量和通过display访问count实例变量
		System.out.println(bd.count);//2
		bd.display();//20
//		//让d2b变量指向原d变量所指向的Dervied对象
		Base d2b = d;
		//访问d2b所指对象的count实例变量
		System.out.println(d2b.count);//2
	}
}

/*
2
2
20
20
2
20
2
请按任意键继续. . .
*/


/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */

class Base
{
	int count = 2;
	public void display()
	{
		System.out.println(this.count);
	}
}
class Derived extends Base
{
	int count = 20;
	@Override
	public void display()
	{
		System.out.println(this.count);
	}
}
public class FieldAndMethodTest
{
	public static void main(String[] args) 
	{
//		//声明、创建一个Base对象
 		Base b = new Base();
//		//直接访问count实例变量和通过display访问count实例变量
 		System.out.println(b.count);//2
 		b.display();//2
		//声明、并创建一个Derived对象
		Derived d = new Derived();
		//直接访问count实例变量和通过display访问count实例变量
		System.out.println(d.count);//20
		d.display();//20
//		//声明一个Base变量,并将Derived对象赋给该变量
		Base bd = new Derived();
//		//直接访问count实例变量和通过display访问count实例变量
		System.out.println(bd.count);//2
		bd.display();//20
//		//让d2b变量指向原d变量所指向的Dervied对象
		Base d2b = d;
		//访问d2b所指对象的count实例变量
		System.out.println(d2b.count);//2
	}
}

/*
2
2
20
20
2
20
2
请按任意键继续. . .
*/


 类似资料:

相关阅读

相关文章

相关问答