JAVA-面向对象下

1.定义一个Father和Child类,并进行测试。 

要求如下: 
1)Father类为外部类,类中定义一个私有的String类型的属性name,name的值为“zhangjun”。 2)Child类为Father类的内部类,其中定义一个introFather()方法,方法中调用Father类的name属性。 
3)定义一个测试类Test,在Test类的main()方法中,创建Child对象,并调用introFather ()方法。 

class Father{
	private String name="zhangjun";
	public class Child{
		public String introFather(){
			return name;
		}
	}
}
public class Test1{
	public static void main(String []args){
		Father.Child test =new Father().new Child();
		System.out.println(test.introFather());
	}
}
2.简述下列程序运行结果: 
class A{ 
    int y=6; 
    class Inner{ 
        static int y=3; 
            void show(){ 
                System.out.println(y); 
                } 
        } 
    } 
class Demo{ 
       public static void main(String [] args){ 
       A.Inner inner=new A().new Inner(); 
       inner.show(); 
           } 
} 

此处的int y定义有错误,应该先改成static final int y=3。Inner是A的内部类,在main方法中通过实例化一个Inner,从而调用Inner里面的show方法。show方法的的println是打印就近y=3;

3、写出下面程序运行结果: 

class A{ 
    public A(){ 
        System.out.println(“A”); 
        } 
} 

class B extends A{ 
        public B(){ 
            System.out.println(“B”); 
        } 
}
    public static void main(String[] args){ 
        B b=new B(); 
    } 
} 

在主方法中实例化一个B,同时B继承了A,在实例化B之前先要实例化A。所以会先执行A中的println命令再执行B中的pringln命令

4、编程题: 
打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。 

class Judge{
	public void output(){
		for(int number=999;number>=100;--number){
			int	hundredPlace=(number)/100;
			int tensPlace=((number)/10)%10;
			int onesPlace=(number)%10;
			if(number == ((int)(Math.pow((float)hundredPlace,3.0)))+
			((int)(Math.pow((float)tensPlace,3.0)))+
			((int)(Math.pow((float)onesPlace,3.0))))
				{	
				System.out.println(number);
				}
			}
		}
	}
public class Test4{
	public static void main(String []args){
		Judge jud =new Judge();
		jud.output();
	}
}
5、编程题: (待修改)
定义一个抽象的"Role"类,有姓名,年龄,性别等成员变量 
1)要求尽可能隐藏所有变量(能够私有就私有,能够保护就不要公有), 
再通过GetXXX()和SetXXX()方法对各变量进行读写。具有一个抽象的play()方法, 
该方法不返回任何值,同时至少定义两个构造方法。Role类中要体现出this的几种用法。 
2)从Role类派生出一个"Employee"类,该类具有Role类的所有成员(构造方法除外), 
并扩展salary成员变量,同时增加一个静态成员变量“职工编号(ID)”。 
同样要有至少两个构造方法,要体现出this和super的几种用法,还要求覆盖play()方法, 
并提供一个final sing()方法。 
3)"Manager"类继承"Employee"类,有一个final成员变量"vehicle" 

在main()方法中制造Manager和Employee对象,并测试这些对象的方法。

abstract class Role{
	private String name;
	private int age;
	private String sexual;
	public Role(){
		System.out.println("1、Role空构造方法");
	}
	public Role(int x){
			System.out.println(x+"+2、单参数够惨方法");
		}	
	//name
	private void SetName(String name){
		this.name=name;
	}
	public String GetName(){
		return name;
	}
	//age
	private void SetAge(String name){
		this.age=age;
	}
	public int GetAge(){
		return age;
	}
	//sexual
	private void SetSexual(String name){
		this.sexual=sexual;
	}
	public String GetSexual(){
		return sexual;
	}
	abstract void play();
}
class Emlopyee extends Role{
	private int salary;
	static int ID;
	public Emlopyee(){
		super();
		System.out.println("Emlopyee的空参数构造");
	}
	public Emlopyee(String name,int age,String sexual,int salary,int ID){
		super(name,age,sexual);
		this.salary=salary;
		Emlopyee.ID = ID;
		System.out.println("传薪水");	
	}
	public void play(){
		System.out.println("覆盖后的play方法");
	}
	final void sing(){
		System.out.println("sing方法");
	}
}
class Manager extends Emlopyee{
	static final String vehicle="自行车";
}
class Test5{
	public static void main(String []args){
		Role role1=new Emlopyee("张三",18,"男",10000,9527);
		role1.GetAge();
		role1.GetSexual();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值