Java 类的学习

发布于:2024-04-25 ⋅ 阅读:(28) ⋅ 点赞:(0)
import java.util.Scanner;

class Student{
    static int totalStudent=0;
    String name;
    int age;
    public Student(String name1,int age1){
        name=name1;
        age=age1;
        Student.totalStudent++;
    }
    public void display(){
        System.out.println("Name:" + name + "Age:" + age);
    }
    public static int getTotalStudent(){
        return totalStudent;
    }
}
public class Main {
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        Student student1=new Student ("小明",16);
        student1.display();
        Student student2=new Student ("大红",99);
        student2.display();
        System.out.println("学生总数:" + Student.totalStudent);
        System.out.println("学生总数:" + Student.getTotalStudent());
    }
}

class Classs{

    double classNum;
    String className;
    int classScore;
    String classBelong;

    public Classs(double classNum1, String className1, int classScore1, String classBelong1)
    {
            classNum = classNum1;
            className = className1;
            classScore = classScore1;
            classBelong = classBelong1;
    }

    public void display(){
        System.out.println("课程编号:" + classNum + "课程名称:" + className + "学分:" + classScore + "课程归属:" + classBelong);
    }
}
class Student{
    double studentNum;
    String studentName;
    String studentSp;
    String studentGrade;
    String studentEmail;
    double studentPhone;
    public Student(double studentNum1, String studentName1, String studentSp1, String studentGrade1,
                   String studentEmail1, double studentPhone1){
        studentNum = studentNum1;
        studentName = studentName1;
        studentSp = studentSp1;
        studentGrade = studentGrade1;
        studentEmail = studentEmail1;
        studentPhone = studentPhone1;
    }
    public void exchangePhone(double phone){
        studentPhone=phone;
    }
    public void display(){
        System.out.println("学号: " + studentNum);
        System.out.println("姓名: " + studentName);
        System.out.println("邮箱: " + studentEmail);
        System.out.println("电话: " + studentPhone);
        System.out.println("班级: " + studentGrade);
        System.out.println("专业: " + studentSp);
    }
}