1.类的继承和方法重写
定义一个基类作为父类,再定义一个继承父类的子类,在子类中重写父类的方法,使用super关键字调用父类的方法,测试其功能。
public class S4_1 {
public static void main(String[] args) {
Student s1=new Student();
s1.show();
System.out.println("\n");
System.out.println(s1.getName());
System.out.println(s1.gender);
System.out.println(s1.getAge());
}
}
class Person{
private String name;
private int age=18;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Person(){
this.name="亮仔";
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void show(){
System.out.println("这是父类的show()方法");
}
}
class Student extends Person{
String gender;
int age=19;
public Student(){
super();
this.gender="女";
}
public Student(String name,String gender,int age){
super(name,age);
this.gender=gender;
}
@Override
public void show(){
super.show();
System.out.println("这是子类的show()方法");
System.out.println("子类中年龄:"+this.age);
System.out.println("父类中年龄:"+super.getAge());
}
}
2.研究生薪资管理(注:在职研究生继承学生类,实现雇员接口)
在学校中,学生每个月需要交相应的生活费(2000元),雇员每个月有相应的工资(1000~3000随机生成),而在职研究生(on-the-job postgraduate)既是雇员又是学生,所以在职研究生既需要交学费又会有工资。下面要求编写一个程序来统计在职研究生的收入与学费,如果收入无法满足交学费,则输出“撸起袖子加油干!”信息。(思考:如果使用抽象类,是否能完成该要求?)
import java.util.Random;
import java.util.Scanner;
public class S4_3 {
public static void main(String[] args) {
double fee;
double salary;
Graduate graduate = new Graduate();
Scanner sc = new Scanner(System.in);
System.out.println("------登录------");
System.out.print("姓名:");
String name = sc.next();
System.out.print("学号:");
String id = sc.next();
System.out.println("----查询如下----");
System.out.println("您的学费是:" + graduate.getFee());
System.out.println("您的工资是:" + graduate.getSalary());
if(graduate.fee > graduate.salary ){
System.out.println("撸起袖子加油干!");
}
else{
System.out.println("可以稍微轻松一点啦!");
}
}
}
class CollegeStudent{
String name;
String id;
double fee =2000;
public CollegeStudent(){
}
public double getFee() {
return fee;
}
}
interface Employee {
//获取职工的工资
double getSalary();
}
//研究生
class Graduate extends CollegeStudent implements Employee{
//设置工资 随机数
Random ra = new Random();
int right = (Math.max(1000,3000)) ;
int left = (Math.min(1000,3000));
double salary = (ra.nextInt(right) + left );
public Graduate() {
this.name = name;
this.id = id;
this.fee = fee;
this.salary = salary;
}
//获得工资
@Override
public double getSalary() {
return salary;
}
//获得学费
public double getFee(double fee) {
return fee;
}
}
3.创建一个抽象交通工具Vehicle类,它有 wheelNum 和 seatNum 两个成员变量以及抽象方法 display()。
类 Bus 和类 Motorcycle 继承自Vehicle类,实现打印成员变量的 display()方法。在主函数中分别生成Bus对象和Motorcycle对象,上转型为Vehicle对象调用 display()方法。
public class S4_4 {
public static void main(String[] args) {
Bus bus = new Bus();
Motorcycle moto =new Motorcycle();
Vehicle ve1 = bus;
Vehicle ve2 =moto;
ve1.display();
ve2.display();
}
}
abstract class Vehicle{
int wheelNum;
int seatNum;
abstract void display();
}
class Bus extends Vehicle{
int wheelNum = 4;
int seatNum = 25;
void display() {
System.out.println("公交车有" + wheelNum +"个轮子,有" + seatNum +"个座位。");
}
}
class Motorcycle extends Vehicle{
int wheelNum = 2;
int seatNum = 1;
void display() {
System.out.println("摩托车有" + wheelNum +"个轮子,有" + seatNum +"个座位。");
}
}
4.经理与员工工资,主要考察多态
某公司的人员分为员工和经理两类,但经理也属于员工中的一类,公司员工和经理都有自己的姓名,年龄,工号、工资、工龄等属性(通过属性无法区分员工和经理)和工资上涨函数。假设每次给员工涨工资一次能涨10%,经理能涨20%。要求利用多态实现给员工和经理涨工资,测试并通过。
public class S4_5 {
public static void main(String[] args) {
Employees em1 = new Employees("小周",18,"21212121",7800,0.5);
Manage ma1 = new Manage("亮仔",19,"2125060277",12000,1.5);
System.out.println("老板:经理亮仔和员工小周干的不错涨工资!");
em1.raiseSalary();
ma1.raiseSalary();
System.out.println("经理亮仔的工资为:" + ma1.salary);
System.out.println("员工小周的工资为:" + em1.salary);
}
}
class Employees{
String name;
int age;
String id;
double salary;
double workingage;
public Employees(String name,int age,String id,double salary,double workingage){
this.name = name;
this.age = age;
this.id = id;
this.salary = salary;
this.workingage = workingage;
}
public void raiseSalary(){
salary = 1.1*salary;
};
}
class Manage extends Employees{
public Manage(String name, int age, String id, double salary, double workingage) {
super(name, age, id, salary, workingage);
}
@Override
public void raiseSalary(){
salary = 1.2*salary;
}
}
5.把下面的代码补充完整,输出结果为“实现了Inner接口的匿名内部类!”,并测试输出结果。
interface Inner{
void introduce();
}
class Outer{
//补齐代码,完成方法主要功能
}
class InnerClassTest{
public static void main(String[] args){
Outer.method().introduce ();
}
}
该题参考天才小小布的文章,讲的很细!
class Outer{
//补齐的代码
public static Inner method(){
return new Inner() {
@Override
public void introduce() {
System.out.println("实现了Inner接口的匿名内部类!");
}
};
}
}
6.设计一个类,在类中能够处理自定义异常类并测试。
import java.util.Scanner;
public class S4_7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的信息!");
System.out.println("姓名:");
String name = sc.next();
System.out.println("身份证号码:");
String id = sc.next();
System.out.println("年龄:");
int age = sc.nextInt();
People p = new People();
try{
p.setAge(age);
System.out.println("-----------信息展示----------");
System.out.println(name+" "+id+" "+age);
}catch (AgeException e1){
e1.getMessage();
}
}
}
class AgeException extends Exception{
public AgeException(){
System.out.println("年龄必须在0~120岁之间!");
}
}
class People{
private String name;
private String id;
private int age;
public void setAge(int age) throws AgeException{
if(age>0 && age<120){
this.age = age;
}else {
throw new AgeException();
}
}
}
家人们,拜托点赞收藏关注呀!