###第1關 類的繼承 package step1; import java.util.Scanner; class Person { /********** Begin **********/ // 自行設計類的實現 //姓名 private String name; //性別 private S ...
第1關 類的繼承
package step1;
import java.util.Scanner;
class Person {
/********** Begin **********/
// 自行設計類的實現
//姓名
private String name;
//性別
private String sex;
//年齡
private int age;
//構造方法
public Person(String name, String sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
}
//重寫toString()方法
public String toString(){
return name + "," + sex + "," + age;
}
/********** End **********/
}
class Student extends Person {
/********** Begin **********/
// 自行設計類的實現
//學號
private String no;
//入學時間
private String enter;
//專業
private String major;
//構造方法
public Student(String name, String sex, int age, String no, String enter, String major){
super(name, sex, age);
this.no = no;
this.enter = enter;
this.major = major;
}
//重寫toString()方法
public String toString(){
return super.toString() + "," + no + "," + enter + "," + major;
}
/********** End **********/
}
class Teacher extends Person {
/********** Begin **********/
// 自行設計類的實現
//職稱
private String pro;
//部門
private String department;
//構造方法
public Teacher(String name, String sex, int age, String pro, String department){
super(name, sex, age);
this.pro = pro;
this.department = department;
}
//重寫toString()方法
public String toString(){
return super.toString() + "," + pro + "," + department;
}
/********** End **********/
}
public class Lab3_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student student = new Student(sc.next(), sc.next(), sc.nextInt(), sc.next(), sc.next(), sc.next());
Teacher teacher = new Teacher(sc.next(), sc.next(), sc.nextInt(), sc.next(), sc.next());
System.out.println("學生基本信息為:" + student);
System.out.println("教師的信息為:" + teacher);
sc.close();
}
}
第2關 上轉型對象的使用,多態的特性
package step2;
abstract class Employee {
public abstract double earnings();
}
class YearWorker extends Employee {
//重寫earnings()方法
/********** Begin **********/
public double earnings() {
return 100000;
}
/********** End **********/
}
class MonthWorker extends Employee {
//重寫earnings()方法
/********** Begin **********/
public double earnings() {
return 10000 * 12;
}
/********** End **********/
}
class WeekWorker extends Employee {
//重寫earnings()方法
/********** Begin **********/
public double earnings() {
return 5000 * 4 * 12;
}
/********** End **********/
}
class Company {
Employee[] employees;
double salaries = 0;
Company(Employee[] employees) {
this.employees = employees;
}
public double salariesPay() {
salaries = 0;
//計算salaries
/********** Begin **********/
for (Employee employee : employees) {
salaries += employee.earnings();
}
/********** End **********/
return salaries;
}
}
public class HardWork {
public static void main(String[] args) {
Employee[] employees = new Employee[20];
for (int i = 0; i < employees.length; i++) {
if(i%3==0)
employees[i] = new WeekWorker();
else if(i%3==1)
employees[i] = new MonthWorker();
else if(i%3==2)
employees[i] = new YearWorker();
}
Company company = new Company(employees);
System.out.println("公司年工資總額:" + company.salariesPay());
}
}
第3關 介面的使用
package step3;
// 自行設計介面和實現類
/********** Begin **********/
// 定義介面
interface Display {
public void display();
}
// 通知類
class Inform implements Display {
// 實現 display() 方法
public void display() {
System.out.println("通知內容");
}
}
// 汽車類
class Car implements Display {
// 實現 display() 方法
public void display() {
System.out.println("汽車油量");
}
}
// 廣告類
class Adervise implements Display {
// 實現 display() 方法
public void display() {
System.out.println("廣告消息");
}
}
/********** End **********/
public class Lab3_3 {
public static void main(String[] args) {
Display[] arr = { new Inform(), new Car(), new Adervise() };
for (Display d : arr) {
d.display();
}
}
}
第4關 綜合應用
Student
package step4;
public class Student {
/********** Begin **********/
private String sno; // 學號
private String sname; // 姓名
private String sdept; // 系別
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSdept() {
return sdept;
}
public void setSdept(String sdept) {
this.sdept = sdept;
}
@Override
public String toString() {
return "學號: " + sno + " 姓名: " + sname + " 系部: " + sdept;
}
/********** End **********/
}
IStudentDAO
package step4;
public interface IStudentDAO {
/********** Begin **********/
void insertStudent(Student student); // 插入學生信息
void updateStudent(Student student); // 更新學生信息
void deleteStudent(String sno); // 刪除學生信息
Student findStudentBySno(String sno); // 根據學號查找學生信息
void displayAllStudent(); // 顯示所有學生信息
/********** End **********/
}
StudentDAOImpl
package step4;
import java.util.ArrayList;
import java.util.List;
public class StudentDAOImpl implements IStudentDAO {
static List<Student> students = new ArrayList<Student>();
/********** Begin **********/
//自行實現所有其它方法
@Override
public void insertStudent(Student student) {
students.add(student);
}
@Override
public void updateStudent(Student student) {
for (Student stu : students) {
if (stu.getSno().equals(student.getSno())) {
stu.setSname(student.getSname());
stu.setSdept(student.getSdept());
break;
}
}
}
@Override
public void deleteStudent(String sno) {
for (Student stu : students) {
if (stu.getSno().equals(sno)) {
students.remove(stu);
break;
}
}
}
@Override
public Student findStudentBySno(String sno) {
for (Student stu : students) {
if (stu.getSno().equals(sno)) {
return stu;
}
}
return null;
}
/********** End **********/
/**
* 顯示所有學生信息記錄參考代碼
*/
@Override
public void displayAllStudent() {
if (students.size() > 0) {
for (Student stu : students) {
System.out.println(stu);
}
}else {
System.out.println("資料庫中無學生記錄!");
}
}
}
MainClass
package step4;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentDAOImpl studentDAOImpl = new StudentDAOImpl();
//1. 插入學生信息
Student stu = new Student();
stu.setSno(scanner.next());
stu.setSname(scanner.next());
stu.setSdept(scanner.next());
studentDAOImpl.insertStudent(stu);
//2. 顯示插入學生信息
System.out.println("1. 插入學生信息如下:");
studentDAOImpl.displayAllStudent();
//3. 更新學生信息
stu.setSname("李四");
stu.setSdept("電腦系");
studentDAOImpl.updateStudent(stu);
System.out.println("2. 更新後學生信息如下:");
System.out.println(studentDAOImpl.findStudentBySno(stu.getSno()));
//4. 刪除指定學生信息
System.out.println("3. 刪除當前學號學生信息:" + stu.getSno());
studentDAOImpl.deleteStudent(stu.getSno());
System.out.println("學生信息已刪除!");
//2. 顯示插入學生信息
System.out.println("5. 顯示所有學生信息:");
studentDAOImpl.displayAllStudent();
scanner.close();
}
}