🏠个人主页: 黑洞晓威
🧑个人简介:大家好,我是晓威,一名普普通通的大二在校生,希望在CSDN中与大家一起成长。🎁如果你也在正在学习Java,欢迎各位大佬来到我的博客查漏补缺呀,如果有哪里写的不对的地方也欢迎诸佬指正啊。
@[Toc]
一、实验目的
1、理解继承的概念;
2、掌握方法的重写;
3、掌握super、final关键字的使用;
4、掌握抽象类和接口的使用;
5、掌握多态的使用;
6、掌握内部类的使用;
7、掌握异常处理方式,能够自定义异常类;
8、了解Object类。
二、实验内容
1、实验题目:类的继承和方法重写
定义一个基类作为父类,再定义一个继承父类的子类,在子类中重写父类的方法,使用super关键字调用父类的方法,测试其功能。
class Person {
public void say(){
System.out.println("我是父类,会被子类的super调用呀");
}
}
class Student extends Person{
public void say(){
System.out.println("我是子类");
super.say();//调用父类的say()方法
}
}
public class S4_1 {
public static void main(String[] args) {
new Student().say();
//调用Student中的say()方法,方法中又调用父类Person中的say()方法
}
}
2、实验题目:final关键字的应用
定义一个类,在类中声明成员变量和成员方法,尝试使用final关键词修饰类中的变量、方法及该类,测试并查看结果,必要时加以注释。
class Person {
private final String name;//只能被定义一次之后无法修改
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void say(){
System.out.println("我是父类成员方法");
}
public final void finalsay(){ //final修饰的方法子类无法重写
System.out.println("我是父类的final方法");
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Student extends Person
//如果Person类用final修饰,extends Person 会报错,final修饰的类无法被继承
{
public Student(String name, int age) {
super(name, age);
}
public void say(){
System.out.println("我是子类重写的非final方法");
}
//报错,无法重写
/*public void finalsay(){
System.out.println("子类的方法");
super.say();
}*/
}
public class S4_2 {
public static void main(String[] args) {
Person student = new Student("Tom",18);
System.out.println("初始化名字为:"+student.getName());
System.out.println("初始化年龄为:"+student.getAge());
student.setAge(19);
System.out.println("修改后年龄为:"+student.getAge());
//没有student.setName,因为name被final修饰初始化后不能更改
student.say();//调用子类重写的父类的say()方法
student.finalsay();//调用父类的finalsay()方法,因为子类无法重写
}
}
3、实验题目:研究生薪资管理
(注:在职研究生继承学生类,实现雇员接口)
在学校中,学生每个月需要交相应的生活费(2000元),雇员每个月有相应的工资(1000~3000随机生成),而在职研究生(on-the-job postgraduate)既是雇员又是学生,所以在职研究生既需要交学费又会有工资。下面要求编写一个程序来统计在职研究生的收入与学费,如果收入无法满足交学费,则输出“撸起袖子加油干!”信息。(思考:如果使用抽象类,是否能完成该要求?)
答:无法实现继承两个类,不能完成要求
interface Empioyee {
int salary =12*(int)(1000+2000*Math.random());//计算全年的工资为多少
}
class Student {
private int cost = 2000*12;//计算一年的学费
public int getCost() {
return cost;
}
}
class Postgraduate extends Student implements Empioyee{
public void judge(){
if(new Student().getCost()>salary){//判断学费与工资
System.out.println("撸起袖子加油干!");
}else {
System.out.println("干得好,打工人!");
}
}
}
public class S4_3 {
public static void main(String[] args) {
Postgraduate postgraduate = new Postgraduate();//创建一个研究生实例
postgraduate.judge();//调用judge方法
}
}
4、实验题目:创建一个抽象交通工具Vehicle类
它有 wheelNum 和 seatNum 两个成员变量以及抽象方法 display()。 类 Bus 和类 Motorcycle 继承自Vehicle类,实现打印成员变量的 display()方法。在主函数中分别生成Bus对象和Motorcycle对象,上转型为Vehicle对象调用 display()方法。
abstract class Vehicle {//抽象类
int wheelNum;
int seatNum;
protected abstract void display();
}
class Bus extends Vehicle{
public Bus(int a, int b) {
this.wheelNum=a;
this.seatNum=b;
}
@Override
public void display(){//重写抽象方法
System.out.println("公交车有"+this.wheelNum+"个轮子和"+this.seatNum+"个座位");
}
}
class Motorcycle extends Vehicle{
public Motorcycle(int a, int b) {
this.wheelNum=a;
this.seatNum=b;
}
@Override//重写抽象方法
public void display(){
System.out.println("摩托车有"+this.wheelNum+"个轮子和"+this.seatNum+"个座位");
}
}
public class S4_4 {
public static void main(String[] args) {
Vehicle bus = new Bus(4,40);
Vehicle motorcycle = new Motorcycle(2, 2);
bus.display();
motorcycle.display();
}
}
5、实验题目:经理与员工工资,主要考察多态
某公司的人员分为员工和经理两类,但经理也属于员工中的一类,公司员工和经理都有自己的姓名,年龄,工号、工资、工龄等属性(通过属性无法区分员工和经理)和工资上涨函数。假设每次给员工涨工资一次能涨10%,经理能涨20%。要求利用多态实现给员工和经理涨工资,测试并通过。
public class S4_5 {
public static void main(String[] args) {
Employee employee = new Employee(1000);
Employee manager = new manager(1000);//多态的实现
System.out.println("原工资为:"+ employee.getSalary());
employee.raise();//调用Employee中的raise方法
manager.raise();//调用manager类中重写的raise方法
System.out.println("员工增长10%后为:"+employee.getSalary());
System.out.println("经理增长20%后为:"+manager.getSalary());
}
}
class Employee{
protected int age;
protected int id;
protected double salary;
protected int workage;
public Employee() {
}
public Employee(double salary) {
this.salary = salary;
}
public void raise(){
this.salary+=this.salary*0.1;
}
public double getSalary() {
return salary;
}
}
class manager extends Employee{
public manager() {
}
public manager(double salary) {
this.salary = salary;
}
public void raise(){//重写Employee中的raise()方法
this.salary+=this.salary*0.2;
}
@Override
public double getSalary() {
return salary;
}
}
6、实验题目:补全代码
把下面的代码补充完整,输出结果为“实现了Inner接口的匿名内部类!”,并测试输出结果。
interface Inner{
void introduce();
}
class Outer{
//补齐代码,完成方法主要功能
}
class InnerClassTest{
public static void main(String[] args){
Outer.method().introduce ();
}
}
interface Inner{
void introduce();
}
class Outer {
public static Inner method(){
return new Inner() {
@Override
public void introduce() {
System.out.println("实现了Inner接口的匿名内部类");
}
};
}
//补齐代码,完成方法主要功能
}
class S4_6{
public static void main(String[] args){
Outer.method().introduce();
}
}
7、实验题目:设计一个类,在类中能够处理自定义异常类并测试。(选做)
import java.util.Scanner;
class myexcept extends Exception {//继承extends
private int detail;
public myexcept(int a) {
this.detail = a;
}
//toString打印异常信息
@Override
public String toString() {
return "出现异常!!"+detail+"大于10";
}
}
class test {
static void moth() throws myexcept {
int i = 0;
System.out.println("请输入一个不大于10的数");
Scanner scanner = new Scanner(System.in);
i = scanner.nextInt();
System.out.println("传递的参数为" + i);
if (i > 10) {
throw new myexcept(i);//如果输入大于10则抛出异常
}
System.out.println("没有出现异常");
}
}
public class S4_7 {
public static void main(String[] args) throws myexcept {
test.moth();
}
}
🎉文章到这里就结束了,感谢诸佬的阅读。🎉
💕欢迎诸佬对文章加以指正,也望诸佬不吝点赞、评论、收藏加关注呀😘