知识点】:

继承(Inheritance)

继承是一种面向对象编程中的机制,允许一个类(子类)继承另一个类(父类)的属性和方法。

继承是面向对象编程的核心概念之一,它允许你创建一个新的类,通过重用现有类的属性和方法,从而促进代码的重用和扩展。

这使得代码重用更容易,并且支持层次化的类结构。

  • 父类和子类:在Java中,你可以定义一个父类(也称为超类或基类),然后创建一个子类(也称为派生类),子类可以继承父类的属性和方法。
    class 父类 {
        // 父类的属性和方法
    }
    
    class 子类 extends 父类 {
        // 子类可以继承父类的属性和方法
    }
    
  • super关键字:在子类中,你可以使用super关键字来调用父类的构造方法或方法。

  • 方法重写:子类可以覆盖(重写)父类的方法,以实现自己的行为。使用@Override注解来确保正确地重写方法。

示例:

// 父类
class Animal {
    String name;

    public Animal(String name) {
        this.name = name;
    }

    void eat() {
        System.out.println(name + "正在吃东西");
    }
}

// 子类
class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }

    void bark() {
        System.out.println(name + "正在叫");
    }
}

在上面的代码示例中,Dog 类继承了 Animal 类的属性和方法。

封装(Encapsulation)

封装是一种将数据(属性)和方法(行为)捆绑在一起并限制外部访问的概念。

封装是一种将数据和方法封装在一个单一单元(类)内部的概念,以保护数据的安全性和完整性。

在Java中,封装通过使用访问修饰符(如private、protected、public)来实现。

  • 私有属性:在Java中,你可以使用private关键字来限制类的属性只能在类内部访问。

  • 公共方法:提供公共方法来访问和修改私有属性,以确保数据的安全性。

示例:

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void sayHello() {
        System.out.println("我叫" + name + ",今年" + age + "岁");
    }
}

在上面的代码示例中,nameage 属性被封装为私有,只能通过公共的 sayHello 方法来访问。

多态(Polymorphism)

多态允许不同的对象对同一消息做出不同的响应。它可以通过继承和接口实现。

  • 方法重载:Java允许你定义多个方法具有相同的名称,但不同的参数列表。这被称为方法重载。

  • 方法重写:如前所述,子类可以重写父类的方法,这也是多态的一种体现。

  • 接口和抽象类:Java中的接口和抽象类允许你定义一组规范,然后不同的类可以实现这些规范,从而实现多态。

示例:

interface Shape {
    void draw();
}

class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("画一个圆形");
    }
}

class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("画一个矩形");
    }
}

在上面的代码示例中,CircleRectangle 类都实现了 Shape 接口,可以调用它们的 draw 方法来实现多态性。

这就是Java中继承、封装和多态的基本概念和代码示例。这些概念是面向对象编程的核心,有助于创建可维护和可扩展的代码。

作业:

有三个系别的学生:英语系,计算机系,文学系

各系学生都有:学号(id:String)、姓名(name:Sting)、性别(sex:boolean)、年龄(age:int)、系别(department:String)属性,但不同系的学生有不同科目成绩,如下所示:

   英语系:  演讲(speech:double),英语写作(writing:double),期中成绩(midScore:double),期末成绩(finalScore:double);

   计算机系:操作能力(operation:double),期中成绩(:double),期末成绩(:double);

   文学系:  阅读理解(reading:double),期中成绩(midScore:double),期末成绩(finalScore:double);

 各系综合成绩评测标准:

    计算机系: 操作能力50%

               期末考试 25%

               期中考试 25%

    英语系:  演讲 40%

               英语写作  20%

               期末考试  20%

               期中考试  20%

    文学系:   阅读理解  40%

               期末考试  30%

               期中考试  30%

对给定的若干学生(自设10个学生,包括:3个英语系、4个计算机系,3个文学系),定义数组存储这些学生,要求采用运行时多态实现统一的综合成绩评定,同时便于以后新增其它系别的学生(业务扩展),并编程实现将学生的信息按以下格式输出:

import java.text.DecimalFormat;
import java.util.Random;

public class test {
    public static void main(String[] args) {
        Random r = new Random();
        String sex;
        student[] arr = new student[10];
	
        println();
    }
    private static void println() {
        //表头
        System.out.println("学号\t姓名\t性别\t年龄\t系别\t\t综合成绩");

        System.out.print(1000001+"\t"+"张三"+"\t"+"男"+"\t"+20+"\t"+"计算机系"+"\t"+85.6);
        System.out.print("\n"+2000002+"\t"+"李四"+"\t"+"女"+"\t"+19+"\t"+"英语系"+"\t\t"+88);
		System.out.print("\n"+"......");
    }

}

class student {
    String name;
    int id;
    String sex;
    int age;
    String department;

    student(String name, int id, String sex, int age, String department) {
        this.name = name;
        this.id = id;
        this.sex = sex;
        this.age = age;
        this.department = department;
    }

    double achievement() {
        return 0;
        }

        class Englishstudent extends student {
        DecimalFormat df = new DecimalFormat("0.00");
        double speech;
        double writing;
        double midScore;
        double finalScore;

        Englishstudent(String name, int id, String sex, int age, String department, double speech, double writing) {
            super(name, id, sex, age, department);
            this.speech = speech;
            this.writing = writing;
            this.midScore = midScore;
            this.finalScore = finalScore;
        }

        double achievement() {
            int finalScorse = 0;
            System.out.println("学号 " + id + " 姓名 " + name + " 性别 " + sex + " 年龄 " + age + " 系别 " + department + " 综合成绩 " + df.format((speech * 2 / 5) + (writing / 5) + (midScore / 5) + (finalScorse / 5)));
            return (speech * 2 / 5) + (writing / 5) + (midScore / 5) + (finalScorse / 5);
        }


        }

        class computerstudent extends student {
        DecimalFormat df = new DecimalFormat("0.00");
        double operation;
        double midScore;
        double finalScore;

        computerstudent(String name, int id, String sex, int age, String department, double operation, double midScore, double finalScore) {
            super(name, id, sex, age, department);
            this.operation = operation;
            this.midScore = midScore;
            this.finalScore = finalScore;
        }

        double achievement() {
            System.out.println("学号 " + id + " 姓名 " + name + " 性别 " + sex + " 年龄 " + age + " 系别 " + department + " 综合成绩 " + df.format((operation / 2) + (midScore / 4) + (finalScore / 4)));
            return (operation / 2) + (midScore / 4) + (finalScore / 4);
        }

        }

     class departmentstudent extends student {
        DecimalFormat df = new DecimalFormat("0.00");
        double reading;
        double midScore;
        double finalScore;

        departmentstudent(String name, int id, String sex, int age, String department, double reading, double midScore, double finalScore) {
            super(name, id, sex, age, department);
            this.reading = reading;
            this.midScore = midScore;
            this.finalScore = finalScore;
        }

        double achievement() {
            String id = new String();
            System.out.println("学号 " + id + " 姓名 " + name + " 性别 " + sex + " 年龄 " + age + " 系别 " + department + " 综合成绩 " + df.format((reading * 40 / 100) + (midScore * 30 / 100) + (finalScore * 30 / 100)));
            return (reading * 40 / 100) + (midScore * 30 / 100) + (finalScore * 30 / 100);
         }

     }
}

10-05 01:38