南邮-JAVA实验课(三)

第三次实验

本次实验主要练习知识点是:
类的继承和派生的概念
super关键词的使用
父类对象和子类对象之间的赋值关系
多态的实现
抽象类和抽象方法的定义和使用
接口的定义和使用
异常的定义和使用

1、类的继承

在前面实验二已定义的学生类Student的基础上,以Student类为父类,为学生类派生出一个子类为大学生类(CollegeStudent)。CollegeStudent 类在学生类上增加一个专业(profession)数据属性;方法上增加获得专业和设置专业两个方法。并对超类中的toString( )方法进行重写,使得CollegeStudent类中的toString( )方法除了显示学生类的信息外,还要显示它的专业属性。编写测试程序的主类。在主类中创建一个Student对象和CollegeStudent对象,并显示或修改这两个对象的属性值。

package javalab.student;
public class Student {
	protected String StuNub,ClassNub,Name,Sex;
	protected int Age;
	public static int count = 0;
	public Student(String StuNub,String ClassNub,String Name,String Sex,int Age){
		this.StuNub = StuNub;
		this.ClassNub = ClassNub;
		this.Name = Name;
		this.Sex = Sex;
		this.Age = Age;
		count++;
	}
	public String get_StuNub() {
		return StuNub;
	}
	public String get_ClassNub() {
		return ClassNub;
	}
	public String get_Name() {
		return Name;
	}
	public String get_Sex() {
		return Sex;
	}
	public int get_Age() {
		return Age;
	}
	public void set_StuNub(String StuNub) {
		this.StuNub  = StuNub;
	}
	public void set_ClassNub(String ClassNub) {
		this.ClassNub  = ClassNub;
	}
	public void set_Name(String Name) {
		this.Name  = Name;
	}
	public void set_Sex(String Sex) {
		this.Sex  = Sex;
	}
	public void set_Age(int Age) {
		this.Age  = Age;
	}
	public static int get_count() {
		return count;
	}
	public String toString() {
		String s = "学号:"+StuNub +",班级:" + ClassNub + ",姓名:" + Name + ",性别:" + Sex + ",年龄:" + Age ;
		return s;
	}
}
package javalab;

import javalab.student.Student;
public class Main {
	public static void main(String args[]) {
		Student std1 = new Student("B16031114", "B160311", "李常委", "男", 21);
		CollegeStudent std2 = new CollegeStudent("B16031188", "B160311", "李伟", "男", 22, "光电子学");
		
		System.out.println("Student对象:\n"+std1.toString());
		System.out.println("CollegeStudent对象:\n"+std2.toString());
		
		std1.set_Age(22);
		std2.setProfession("光电信息科学与工程");
		
		System.out.println("信息修改以后为:");
		System.out.println("Student对象:\n"+std1.toString());
		System.out.println("CollegeStudent对象:\n"+std2.toString());
	}
}

class CollegeStudent extends Student{
	private String profession;
	public CollegeStudent(String StuNub, String ClassNub, String Name, String Sex, int Age, String profession) {
		super(StuNub, ClassNub, Name, Sex, Age);
		this.profession = profession;
	}
	public void setProfession(String profession) {
		this.profession = profession;
	}
	public String getProfession() {
		return this.profession;
	}
	public String toString () {
		return "学号:"+StuNub +",班级:" + ClassNub + ",姓名:" + Name + ",性别:" + Sex + ",年龄:" + Age +",专业:" + profession;
	}
}

2、继承和多态

设计一个人员类(Person),其中包含一个方法pay,代表人员的工资支出。再从Person类派生出助教类(Assistant)、讲师类(Instructor)、副教授类(Assistant Professor)和教授类(Professor)。其中:工资支出=基本工资+授课时数*每课时兼课金。
但助教基本工资为800,每课时兼课金25,讲师基本工资为1000,每课时兼课金35,
副教授基本工资为1200,每课时兼课金40,教授基本工资为1400,每课时兼课金50。
① 将Person定义为抽象类,pay为抽象方法,设计程序实现多态性。
② 将Person定义为接口,设计程序实现多态性。

Person定义为普通类:

	public class Main {
		public static void main(String args[]) {
			Assistant assistant = new Assistant(10);
			Instructor instructor = new Instructor(10);
			Assistant_Professor assistant_professor = new Assistant_Professor(10);
			Professor professor = new Professor(10);
			
			System.out.println("助教工资支出:" + assistant.pay());
			System.out.println("讲师工资支出:" + instructor.pay());
			System.out.println("副教授工资支出:" + assistant_professor.pay());
			System.out.println("教授工资支出:" + professor.pay());
		}
	}
	
	class Person{
		protected int payload, base_pay, teaching_hours, class_pay;
		public int pay() {
			payload = base_pay + teaching_hours*class_pay;
			return payload;
		};
	}
	
	class Assistant extends Person{
		
		public Assistant(int teaching_hours) {
			base_pay = 800;
			class_pay = 25;
			this.teaching_hours = teaching_hours;
		}
	} 
	
	class Instructor extends Person{
		public Instructor(int teaching_hours) {
			base_pay = 1000;
			class_pay = 35;
			this.teaching_hours = teaching_hours;
		}
	} 
	
	class Assistant_Professor extends Person{
		public Assistant_Professor(int teaching_hours) {
			base_pay = 1200;
			class_pay = 40;
			this.teaching_hours = teaching_hours;
		}
	}
	
	class Professor extends Person{
		public Professor(int teaching_hours) {
			base_pay = 1400;
			class_pay = 50;
			this.teaching_hours = teaching_hours;
		}
	}

Person定义为抽象类:

	public class Main {
		public static void main(String args[]) {
			Assistant assistant = new Assistant(10);
			Instructor instructor = new Instructor(10);
			Assistant_Professor assistant_professor = new Assistant_Professor(10);
			Professor professor = new Professor(10);
			
			System.out.println("助教工资支出:" + assistant.pay());
			System.out.println("讲师工资支出:" + instructor.pay());
			System.out.println("副教授工资支出:" + assistant_professor.pay());
			System.out.println("教授工资支出:" + professor.pay());
		}
	}
	
	abstract class Person{
		abstract public int pay();
	}
	
	class Assistant extends Person{
		private int payload, base_pay, teaching_hours, class_pay;
		public Assistant(int teaching_hours) {
			base_pay = 800;
			class_pay = 25;
			this.teaching_hours = teaching_hours;
		}
		public int pay() {
			payload = base_pay + teaching_hours*class_pay;
			return payload;
		}
	} 
	
	class Instructor extends Person{
		private int payload, base_pay, teaching_hours, class_pay;
		public Instructor(int teaching_hours) {
			base_pay = 1000;
			class_pay = 35;
			this.teaching_hours = teaching_hours;
		}
		public int pay() {
			payload = base_pay + teaching_hours*class_pay;
			return payload;
		}
	} 
	
	class Assistant_Professor extends Person{
		private int payload, base_pay, teaching_hours, class_pay;
		public Assistant_Professor(int teaching_hours) {
			base_pay = 1200;
			class_pay = 40;
			this.teaching_hours = teaching_hours;
		}
		public int pay() {
			payload = base_pay + teaching_hours*class_pay;
			return payload;
		}
	}
	
	class Professor extends Person{
		private int payload, base_pay, teaching_hours, class_pay;
		public Professor(int teaching_hours) {
			base_pay = 1400;
			class_pay = 50;
			this.teaching_hours = teaching_hours;
		}
		public int pay() {
			payload = base_pay + teaching_hours*class_pay;
			return payload;
		}
	}

Person定义为接口:

public class Main {
	public static void main(String args[]) {
		Assistant assistant = new Assistant(10);
		Instructor instructor = new Instructor(10);
		Assistant_Professor assistant_professor = new Assistant_Professor(10);
		Professor professor = new Professor(10);
		
		System.out.println("助教工资支出:" + assistant.pay());
		System.out.println("讲师工资支出:" + instructor.pay());
		System.out.println("副教授工资支出:" + assistant_professor.pay());
		System.out.println("教授工资支出:" + professor.pay());
	}
}

interface Person{
	abstract public int pay();
}

class Assistant implements Person{
	private int payload, base_pay, teaching_hours, class_pay;
	public Assistant(int teaching_hours) {
		base_pay = 800;
		class_pay = 25;
		this.teaching_hours = teaching_hours;
	}
	public int pay() {
		payload = base_pay + teaching_hours*class_pay;
		return payload;
	}
} 

class Instructor implements Person{
	private int payload, base_pay, teaching_hours, class_pay;
	public Instructor(int teaching_hours) {
		base_pay = 1000;
		class_pay = 35;
		this.teaching_hours = teaching_hours;
	}
	public int pay() {
		payload = base_pay + teaching_hours*class_pay;
		return payload;
	}
} 

class Assistant_Professor implements Person{
	private int payload, base_pay, teaching_hours, class_pay;
	public Assistant_Professor(int teaching_hours) {
		base_pay = 1200;
		class_pay = 40;
		this.teaching_hours = teaching_hours;
	}
	public int pay() {
		payload = base_pay + teaching_hours*class_pay;
		return payload;
	}
}

class Professor implements Person{
	private int payload, base_pay, teaching_hours, class_pay;
	public Professor(int teaching_hours) {
		base_pay = 1400;
		class_pay = 50;
		this.teaching_hours = teaching_hours;
	}
	public int pay() {
		payload = base_pay + teaching_hours*class_pay;
		return payload;
	}
}
  • 在课堂上老师强调了多态,0-0,多态我没使用哇!!!!!
  • 以上代码的主方法内的程序段替换一下就好啦!

源代码的主方法:

	public static void main(String args[]) {
		Assistant assistant = new Assistant(10);
		Instructor instructor = new Instructor(10);
		Assistant_Professor assistant_professor = new Assistant_Professor(10);
		Professor professor = new Professor(10);
			
		System.out.println("助教工资支出:" + assistant.pay());
		System.out.println("讲师工资支出:" + instructor.pay());
		System.out.println("副教授工资支出:" + assistant_professor.pay());
		System.out.println("教授工资支出:" + professor.pay());
	}

替换为:

public static void main(String args[]) {
		Person person;
		person = new Assistant(10);
		System.out.println("助教工资支出:" + person.pay());
		
		person = new Instructor(10);
		System.out.println("讲师工资支出:" + person.pay());
		
		person = new Assistant_Professor(10);
		System.out.println("副教授工资支出:" + person.pay());
		
		person = new Professor(10);
		System.out.println("教授工资支出:" + person.pay());	}
}

3、异常的定义和使用

从键盘输入两个数,进行相除,显示商。当输入串中含有非数字时或除数为0时,通过异常处理机制,使程序能正确运行。

import java.util.Scanner;

public class Main {
	public static void chufa(String a, String b) throws StrZeroNumException{
		int chushu = 0, beichushu = 0;
		boolean sflag = false,zflag = false;
		try{
			beichushu = Integer.parseInt(a);
			chushu = Integer.parseInt(b);
		}catch(Exception e){
			sflag = true;
			StrZeroNumException ze = new StrZeroNumException();
			throw ze;
		}
		if(chushu == 0) {
			zflag = true;
			StrZeroNumException ze = new StrZeroNumException(chushu);
			throw ze;
		}
		if((!zflag)&&(!sflag)) {
			System.out.println(beichushu+"/" + chushu + "=" + beichushu/chushu);
		}
	}
	public static void main(String args[]) {
		String a,b;
		Scanner s = new Scanner(System.in);
		a = s.next();
		b = s.next();
		try {
			chufa(a, b);
		}catch(StrZeroNumException ze){
			System.out.println(ze.toString());
		}
	}
}

class StrZeroNumException extends Exception{
	String message;
	public StrZeroNumException(int a) {
		message = "除数不能为0";
	}
	public StrZeroNumException() {
		message = "输入串中含有非数字";
	}
	public String toString(){
		return message;
	}
}
  • 需要学习异常的定义的用以上方法verygood
  • 其实我们可以直接使用已有的异常来捕获它们,经测试代码如下:
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
	public static void main(String args[]){
		int a = 0,b = 0;
		Scanner s = new Scanner(System.in);
		try{
			a = s.nextInt();
			b = s.nextInt();
			s.close();
			a = a/b;
		}catch(InputMismatchException e){
			System.out.println("输入的有非数字类型!");
		}catch (ArithmeticException e) {
			System.out.println("除数为0!");
		}
	}
}

  • 实验目录:
  • 南邮-JAVA实验课(一)
  • 南邮-JAVA实验课(二)
  • 南邮-JAVA实验课(三)
  • 南邮-JAVA实验课(四)

你可能感兴趣的:(课程实验)