1.定义一个Father和Child类,并进行测试。
要求如下:
1)Father类为外部类,类中定义一个私有的String类型的属性name,name的值为“zhangjun”。
要求如下:
1)Father类为外部类,类中定义一个私有的String类型的属性name,name的值为“zhangjun”。
2)Child类为Father类的内部类,其中定义一个introFather()方法,方法中调用Father类的name属性。
3)定义一个测试类Test,在Test类的main()方法中,创建Child对象,并调用introFather ()方法。
3)定义一个测试类Test,在Test类的main()方法中,创建Child对象,并调用introFather ()方法。
class Father{
private String name = "zhangjun";
class Child{
void introFather(){
System.out.println(name);
}
}
}
public class Test{
public static void main(String[] args){
Father.Child child = new Father().new Child();
child.introFather();
}
}
2.简述下列程序运行结果
class A{
int y=6;
class Inner{
static int y=3;
void show(){
System.out.println(y);
}
}
}
class Test{
public static void main(String [] args){
A.Inner inner=new A().new Inner();
inner.show();
}
}
编译错误,非静态内部类中不能有静态属性
将static关键字去掉,结果为3,就近原则。
3.写出下面程序运行结果
class A{
public A(){
System.out.println("A");
}
}
class Test extends A{
public Test(){
System.out.println("B");
}
public static void main(String[] args){
Test b=new Test();
}
}
在调用子类的构造方法之前会先调用父类的构造方法,所以运行结果先输出A,在输出B
4.编程题:
打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
public class Test{
public static void main(String[] args){
Flower();
}
static void Flower(){
int b, t, h = 0;
for(int i = 100; i < 1000; i++){
b = i % 10;
t = i / 10 % 10;
h = i / 100;
if(Math.pow(b, 3) + Math.pow(t, 3) + Math.pow(h, 3) == i){
System.out.print(i + " ");
}
}
}
}
5.编程题:
定义一个抽象的"Role"类,有姓名,年龄,性别等成员变量
1)要求尽可能隐藏所有变量(能够私有就私有,能够保护就不要公有),
再通过GetXXX()和SetXXX()方法对各变量进行读写。具有一个抽象的play()方法,
该方法不返回任何值,同时至少定义两个构造方法。Role类中要体现出this的几种用法。
2)从Role类派生出一个"Employee"类,该类具有Role类的所有成员(构造方法除外),
并扩展salary成员变量,同时增加一个静态成员变量“职工编号(ID)”。
同样要有至少两个构造方法,要体现出this和super的几种用法,还要求覆盖play()方法,
并提供一个final sing()方法。
3)"Manager"类继承"Employee"类,有一个final成员变量"vehicle"
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 char sex;
public Role(){
System.out.println("Role无参构造");
}
public Role(String name, int age, char sex){
this();//调用本类无参构造
this.name = name;//this表示本类属性
this.age = age;
this.sex = sex;
}
public String GetName(){
return this.name;
}
public void SetName(String name){
this.name = name;
}
public int GetAge(){
return this.age;
}
public void SetAge(int age){
this.age = age;
}
public char GetSex(){
return this.sex;
}
public void SetSex(char sex){
this.sex = sex;
}
public abstract void play();
}
class Employee extends Role{
private int salary;
private static int id;
public Employee(){
System.out.println("Employee无参构造");
}
public Employee(String name, int age, char sex, int salary, int id){
super(name, age, sex);
this.salary = salary;
this.id = id;
}
public void play(){
System.out.println(this.GetName() + "play");//表示当前对象
}
public final void sing(){//不能被子类所覆写
System.out.println(this.GetName() + this.GetAge() + "sing");
}
}
class Manager extends Employee{
private final String vehicle = "Bmw";//final修饰的变量在类中必须初始化
public void play(){
super.play();//调用父类被覆写的方法
System.out.println("子类play方法");
}
// public void sing(){
// super.sing();
// System.out.println("Manage的sing方法");
// }//编译错误
}
public class Test{
public static void main(String[] args){
Employee em = new Employee("张三", 28, 'n', 10000, 101);
em.play();
em.sing();
Manager ma = new Manager();
ma.play();
em.sing();
}
}