(一)观察下列程序的输出结果,并分析静态变量和实例变量是如何访问的?
class Example05_1 {
int a = 1;
static int b = 2;
}
class Example05_1Demo {
public static void main(String args[]) {
System.out.println("b=" + Example05_1.b);
Example05_1.b *= 2;
Example05_1 o1 = new Example05_1 ();
o1.a = 10;
System.out.println("o1.a="+ o1.a);
System.out.println("b="+ o1.b);
Example05_1 o2=new Example05_1 ();
System.out.println("o2.a=" + o2.a);
System.out.println("b="+ o2.b);
}
}
其中在类Example05_1中定义了两个成员变量a、b,其中变量b有static修饰,所以变量b为静态变量,如果想访问静态变量b,通过类名来访问Example05_1.b即可。如果想访问实例变量a,就要通过对象实例来访问o1.a或o2.a。静态变量b是在各对象实例间共享,而实例变量a是每个对象实例都独有的
(二)设计一个人员类(Person),其中包含一个方法pay,代表人员的工资支出。再从Person类派生出教师类(Teacher)和大学生类(CollegeStudent)其中:
教师:工资支出=基本工资+授课时数*30
大学生:奖学金支出
将人员类定义为抽象类,pay为抽象方法,设计程序实现多态性。
package example1;
//抽象类Person
abstract class Person {
protected String name;
public Person(String name) {
this.name = name;
}
abstract public double pay();
}
package example1;
class Teacher extends Person {
private double basic_salary; // 基本工资
private double teaching_Hours; // 授课时数
public Teacher(String name, double basic_salary, double teaching_Hours) {
super(name);
this.basic_salary = basic_salary;
this.teaching_Hours = teaching_Hours;
}
@Override
public double pay() {
return basic_salary + teaching_Hours * 30;
}
}
package example1;
class CollegeStudent extends Person {
private double scholarship; // 奖学金
public CollegeStudent(String name, double scholarship) {
super(name);
this.scholarship = scholarship;
}
@Override
public double pay() {
return scholarship;
}
}
(三)分析下面程序,使用System.out.println() 分别实现sleep()和message()方法,并增加main方法打印输出结果,掌握接口的概念与使用方法。
interface Sittable {
void sit();
}
interface Lie {
void sleep();
}
interface HealthCare {
void massage();
}
class Chair implements Sittable {
public void sit() {
System.out.println("This is sit method");
}
}
/*
* interface Sofa extends Sittable,Lie //接口可以实现多重继承,用逗号相隔。 { }
*/
class Sofa extends Chair implements Lie, HealthCare// 一个类既可以从父类中继承同时又可以实现多个接口。
{
public void sleep() { // 完善你的代码
System.out.println("This is sleep method");
}
public void massage() { // 完善你的代码
System.out.println("This is massage method");
}
public static void main(String[] args) {
Sofa sofa = new Sofa();
sofa.sleep();
sofa.massage();
}
}
(四)根据实验输出结果完善程序,掌握如何在外部类中访问内部类
package example3;
class Outer {
class Inner {
int i = 50;
}
}
public class InnerClassTest {
public static void main(String[] args) {
Outer on1=new Outer();
// 完善你的代码
System.out.println("i is :" + on1.i);
}
}
(五)设计一个银行帐户类,成员变量包括账号、储户姓名、开户时间、身份证号码、存款余额等帐户信息,成员方法包括存款、取款操作。
package example4;
import java.text.SimpleDateFormat;//引入时间包
public class Bank_Account {
private String account; // 账户
private String name; // 姓名
private String time; // 开户时间
private String Id_number; // 身份证号码
private double balance; // 存款余额
public Bank_Account(String account, String name, double balance) {
this.account = account;
this.name = name;
this.balance = balance;
// 获取系统当前时间并将时间格式化位指定格式
this.time = new SimpleDateFormat("yyyy年MM月dd日,HH时mm分ss秒").format(new java.util.Date());
}
// 获取账号方法
public String getAccount() {
return account;
}
// 获取姓名方法
public String getName() {
return name;
}
// 获取开户时间方法
public String getTime() {
return time;
}
// 获取身份证号码方法
public String getId_number() {
return Id_number;
}
// 获得余额方法
public double getBalance() {
return balance;
}
// 存款方法
public void deposit(double amount) {
this.balance += amount;
}
// 取款方法
public void Withdrawal(double amount) {
if (balance < amount) {
System.out.println("余额不足,不能取款");
} else {
this.balance -= amount;
}
}
}
(六)内部类主要分为哪几种?请简单描述每一类。
①内部类主要分为4种:静态内部类、成员内部类、局部内部类、匿名内部类;
②静态内部类:被声明为static的内部类,不能访问外部类的普通成员变量,只能访问外部类中的静态成员变量和静态方法;
③成员内部类:去掉关键字static即为成员内部类,可以自由的引用外部类的属性和方法;
④局部内部类:定义在一个代码块内的类,作用范围为其所在的代码块,局部类类似于局部变量一样,不能被public、protected、private以及static修饰,只能访问方法中定义为final类型的局部变量;
⑤匿名内部类:一种没有类名的内部类,不使用关键字class、extends、implements,它必须继承其他类或实现其它接口。(七)关键字static可以修饰哪些类的组成部分?
①用来修饰成员变量,将其变为类的成员,从而实现所有对象对于该成员的共享;
②用来修饰成员方法,将其变为类方法,可以直接使用“类名.方法名”的方式调用,常用于工具类;(八)抽象方法有什么特点?抽象方法的方法体在何处定义?定义抽象方法有什么好处?
①抽象方法的特点:
a. 抽象方法是一个不完整的方法,没有方法体。
b. 抽象方法一定是抽象类,抽象的类里不一定有抽象方法,但接口中的方法一定是抽象方法。
c. 抽象方法和类都需要由abstract 修饰。
d. 抽象类不能通过new来实例化,抽象类中的抽象方法要被使用,必须由子类复写所有的抽象方法后,建立子类对象调用。如果子类只覆盖了部分抽象方法,那么该子类还是一个抽象类。
②抽象方法一般都定义在父类里面。
③抽象方法的好处就在于能让子类做一些特定的事。(九)final修饰符可以用来修饰什么?被final修饰符修饰后有何特点?
①final修饰符可以修饰类、方法、成员变量。
②final修饰类的时候,说明该类不能被继承。
③final修饰方法的时候,说明该方法不能被重写。
④当final修饰成员变量时,有两种情况:
a.如果修饰的是基本类型,说明这个变量是不能重新赋值的;
b.如果修饰的是引用类型,该变量所的引用不能变,但引用所代表的对象内容是可变的。(十)创建接口使用什么关键字?接口可以有父接口吗?试书写语句创建—个名为MyInterface的接口,它是继承了MySuperInterface1和MySuperInterface2两个接口的子接口。之后再写一个具体类实现MyInterface接口。
①创建接口使用interface关键字;
②接口可以有父接口;
package example5;
interface MySuperInterface1 {
}
interface MySuperInterface2 {
}
interface MyInterface extends MySuperInterface1, MySuperInterface2 {
}
public class InterfaceDemo {
public static void main(String[] args) {
MyInterface myInterface = new MyInterface() {
};
}
}