一、实验目的与要求
1、深刻理解面向对象的三大特征;
2、熟练掌握关键super、final的含义及用法;
3、理解什么是抽象类、抽象方法,以及两者间的关系;
4、深刻理解接口的概念、作用,以及与抽象类的差别;
5、类型的转换(向上和向下转换),运算符 instanceof的使用;
6、深刻理解、掌握动态多态性(难点重点);
7、初步了解对象间进行消息传递的方式(两种一是乙类对象作为甲类的成员,二是对象作为方法的参数,本质是一样的);
8、逐步掌握异常的分类、处理方法。
二、实验原理
1、面向对象的三大特征
面向对象的3大特征是封装、继承、多态。
封装:在定义一个类时都需要认真思考,类是现实世界中事物的属性和对这些属性进行操作的方法的集合体。因此一个类应该包括数据成员和函数成员,仅仅只含一种成员的类很少(除非特别需要),一般一个属性对应一对set、get,但并非set、get方法一定会成对出现。如何去进行类的封装呢?可以从操作反推属性的确定,即要完成哪些操作,这些操作的进行需要什么数据、得到怎样的结果。
继承:子类继承了父类非私有的成员,(实际上子类继承了父类的所有成员,只是私有的成员在子类中被隐藏了,通过方法还是可以访问到的)。可定义成员的访问权限时,不要把问题复杂化,即在实际应用中大多仅使用公共和私有权限。子类重写父类的方法时,不能缩小该方法的访问权限,(如果是缩小的、则子类的该方法就没有意义了,因为访问不到嘛)。
多态性:包含静态多态和动态多态,一般是指动态多态性。静态多态通过函数的重载实现,较为简单。困难的是动态多态,通过父子类、接口来实现。一般是父类的引用或者接口的引用指向子类的对象的形式。出现在赋值、做参数、方法的调用等位置。务必深刻理解。
三、实验内容、过程、结果(带*的本次不做,下次一起)
1、定义父类Person,派生出Teacher、Student。请选取合适的成员变量、方法。(考虑使用super、final、static关键字)
package test04;
public class Test {
public static void main(String[] args){
Student s = new Student();
s.eat();
s.sleep();
System.out.println("---------");
Teacher t = new Teacher();
t.eat();
t.sleep();
t.teach();
}
}
class Person{
String name;
void eat(){
System.out.println("吃饭");
}
void sleep(){
System.out.println("睡觉");
}
}
class Student extends Person{
int sid; //学号
}
class Teacher extends Person{
int tid; //工号
void teach(){
System.out.println("老师教课");
}
}
2、定义抽象类Shape(含求面积的抽象方法,常量PI),派生出圆、三角形(或者长方形、正方形、梯形);由圆派生出圆锥体求体积。
package test04;
class Shape {
public double pi;
public double GetCircle (double pi) {
double s=3.14*3.14*pi;
return s;
}
public static void main(String [] args) {
Circle a=new Circle(3);
Tangle b=new Tangle(3);
System.out.println(a.GetCircle(3));
System.out.println(b.GetTangle(3));
}
public double GetTangle (double pi) {
double s=pi*pi;
return s;
}
public Shape(double pi) {
super();
this.pi = pi;
}
}
class Circle extends Shape
{
public Circle(double pi) {
super(pi);
}
}
class Tangle extends Shape
{
public Tangle(double pi) {
super(pi);
}
}
*3、什么是适配器?定义一个接口包含求面积、体积的抽象方法,由此定义适配器,通过重写实现求圆、三角形的面积、圆锥体的体积。
适配器就是继承接口的类做父类 派生出的子类能够重写父类的方法
package test04;
import java.util.Scanner;
interface Area {
abstract double mianji(double x) ;
abstract double mianji(double y,double z) ;
abstract double tiji(double r,double h) ;
public static void main (String [] args) {
Scanner a=new Scanner (System.in);
double x=a.nextDouble();
Scanner b=new Scanner (System.in);
double y=b.nextDouble();
Scanner c=new Scanner (System.in);
double z=c.nextDouble();
Scanner d=new Scanner (System.in);
double r=d.nextDouble();
Scanner e=new Scanner (System.in);
double h=e.nextDouble();
area1 area2= new area1();
System.out.println(area2.mianji(x));
System.out.println(area2.mianji(y,z));
System.out.println(area2.mianji(r,h));
}
}
class area1 implements Area{
public double mianji( double x) {
double s1=3.14*x*x;
return s1;
}
public double mianji( double y,double z) {
double s2=0.5*z*y;
return s2;}
public double tiji(double r,double h) {
double s3=1/3*3.14*r*r*h;
return s3;
}
}
4、根据Person类的属性、方法、Teacher派生类的属性方法,实现多态性(重点考查哪些能被访问、那些不能访问,及其规则);
package test04;
class Person {
public String name;
public String work;
public int leg;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
public Person(String name, String work) {
this.name = name;
this.work = work;
}
}
class Teacher extends Person {
public Teacher(String name, String work, String edu) {
super(name, work);
}
public String edu;
public void setWork(String teacher) {
this.work = teacher;
}
public String getEdu() {
return edu;
}
public void setEdu(String edu) {
this.edu = edu;
}
}
class Student extends Teacher {
public Student(String name, String work,String edu) {
super(name, work, edu);
}
public static void main(String[] args) {
Person p = new Teacher("candice", "teacher", "graduate");
System.out.println(p.getWork());
System.out.println(p.getName());
}
}
*5、使用Shape类、接口的数组,求圆、三角形的面积,实现多态性;
package test04;
public class text{
public static void main( String [] args)
{
try {
class Shape {
public double pi;
public double GetCircle (double pi) {
double s=3.14*3.14*pi;
return s;
}
public double GetTangle (double pi) {
double s=pi*pi;
return s;
}
public Shape(double pi) {
super();
this.pi = pi;
}
}
class Circle extends Shape
{
public Circle(double pi) {
super(pi);
}
}
class Tangle extends Shape
{
public Tangle(double pi) {
super(pi);
}
public void main(String [] args ) {
Circle a=new Circle(3);
Tangle b=new Tangle(3);
System.out.println(a.GetCircle(3));
System.out.println(b.GetTangle(3));
}
}
}
catch(Exception e){
e.printStackTrace();//打印错误//
System.out.println("系统出错");
}
finally {
System.out.println("最后");
}
}
}
*6、通过一个数组的值和下标、使用几种方式验证异常处理。
package test04;
public class testFinally {
public static void main(String args[ ]) { int a,b=0;
for(int i=0;i<=3;i++){
System.out.println("Test No: "+(i+1));
try{ switch(i){
case 0: a=3/b; break;
case 1: int c[ ]=new int[10];c[10]=0;break;
case 2: char ch="ABC".charAt(99);break;
case 3: return;
}
}
catch(ArithmeticException e) {
System.out.println("零作除数!");
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界!");
}catch(IndexOutOfBoundsException e) {
System.out.println("下标越界!");
}finally{ System.out.println("在finally块中!"); }
}
}
}
7、实际应用(继承、重载)。
定义Employee(员工)类,由员工类派生出SaleMan(销售员)类和Manager(经理)类。要求:
(1)在员工类中包含有编号(num)、姓名(name)、工资(basicSalary)和奖金(prize)。在销售员类中还包含销售提成比例(deductRate)和个人销售额(personAmout),在经理类中还包含经理提成比例(totalDeductRate)和总销售额(totalAmount)。
(2)各类人员的实发工资公式如下:
员工实发工资=基本工资+奖金
销售员 =基本工资+奖金+个人销售额*销售提成比例
经理 =基本工资+奖金+总销售额*经理提成比例
(3)每个类都有构造函数、输出基本信息的show方法、输出工资的showSalary的方法。
package test04;
public class Employee {
public int num;
public String name;
public int salary;
public int prize;
public int showSalary(int salary,int prize) {
int s;
s=salary+prize;
return s;
}
public static void main (String []args) {
SaleMan n= new SaleMan(1,"Candice",5000,500,0.5,800);
Manager m= new Manager(2,"Lucy",9000,1000,0.5,800);
System.out.println("编号"+n.getNum() );
System.out.println("姓名"+n.getName() );
System.out.println("基本工资"+n.getPrize());
System.out.println("个人销售额"+n.getPersonamout());
System.out.println("销售提成比例"+n.getRate() );
System.out.println("实际工资"+n.showSalary(5000, 500, 0.5, 800) );
System.out.println("编号"+m.getNum() );
System.out.println("姓名"+m.getName() );
System.out.println("基本工资"+m.getPrize());
System.out.println("个人销售额"+m.getTotalamount());
System.out.println("销售提成比例"+m.getTotalrate());
System.out.println("实际工资"+m.showSalary(9000, 1000, 0.5, 800) );
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getPrize() {
return prize;
}
public void setPrize(int prize) {
this.prize = prize;
}
public Employee(int num, String name, int salary, int prize) {
super();
this.num = num;
this.name = name;
this.salary = salary;
this.prize = prize;
}
}
class SaleMan extends Employee{
public SaleMan(int num, String name, int salary, int prize) {
super(num,name,salary,prize);
}
public double rate;
public int personamout;
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public int getPersonamout() {
return personamout;
}
public void setPersonamout(int personamout) {
this.personamout = personamout;
}
public SaleMan(int num, String name, int salary, int prize, double rate, int personamout) {
super(num, name, salary, prize);
this.rate = rate;
this.personamout = personamout;
}
public double showSalary(int salary ,int prize ,double rate,int personamout) {
double a;
a= salary+ prize + rate*personamout;
return a;
}
}
class Manager extends Employee{
public Manager(int num,String name,int salary, int prize) {
super(num,name,salary,prize);
}
public double totalrate;
public int totalamount;
public double showSalary(int salary ,int prize ,double totalrate, int totalamount ) {
double b;
b=salary + prize + totalrate + totalamount;
return b;
}
public double getTotalrate() {
return totalrate;
}
public void setTotalrate(double totalrate) {
this.totalrate = totalrate;
}
public int getTotalamount() {
return totalamount;
}
public void setTotalamount(int totalamount) {
this.totalamount = totalamount;
}
public Manager(int num, String name, int salary, int prize, double totalrate, int totalamount) {
super(num, name, salary, prize);
this.totalrate = totalrate;
this.totalamount = totalamount;
}
}
四、实验总结与体会