JAVA-008类和对象

类和对象的关系

类是具有相同属性和行为的一组对象的抽象。
对象是类的实例化。
JAVA中 万物皆对象,对象因为关注而产生
类是引用数据类型(之前学过的String就是一个类)

类名-Java文件名-class文件名的关系

  • 公共类的类名必须与Java文件名相同。
  • class文件名必须与类名相同。
  • 一个Java文件可以有n个类(一个public类),n个class文件。

类的定义

public class 类名{属性
行为(方法)
}

必须用this.的情况

当局部变量与全局变量重复时候,则要求操作全局属性时,就必须给属性加上this. ;否则编译器根据就近原则定义为局部变量

定义一个狗类;

public class Dog {

    //属性--值数据
    //全局
    //成员
    public String name;

    public int age;

    public final int HEART_NUM = 1;

    //行为
    public void bark(){
        System.out.println(this.name + "叫:汪汪汪汪汪汪");
    }

    public String feed(String food){
        return "来," + this.name + ",吃" + food + "!";
    }

}

//测试类

public class TestMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    String str = "来福";
    Dog dog = new Dog();
    Dog dog1 = new Dog();
    dog.name = str;
    dog1.name = "旺财";
    str = dog1.feed("骨头");
    System.out.println(str);
    str = dog.feed("狗粮");
    System.out.println(str);

    new Dog().feed("米饭");//匿名对象--只能使用1次

事实上类属性中的变量都应该是private型。所以在要求中,所有的类属性变量定义为private。例如:

public class ClassA {

    private String name;

    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }   
}

采用get/set方法访问和修改属性值。比public定义 更加的安全和灵活

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来帮你创建一个Java程序来实现这个需求。 首先,我们先定义一个学生类,包括学号、姓名、三门课程的成绩: ```java public class Student { private String id; // 学号 private String name; // 姓名 private double javaScore; // Java成绩 private double cScore; // C语言成绩 private double dsScore; // 数据结构成绩 // 构造方法 public Student(String id, String name, double javaScore, double cScore, double dsScore) { this.id = id; this.name = name; this.javaScore = javaScore; this.cScore = cScore; this.dsScore = dsScore; } // getter和setter方法 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getJavaScore() { return javaScore; } public void setJavaScore(double javaScore) { this.javaScore = javaScore; } public double getcScore() { return cScore; } public void setcScore(double cScore) { this.cScore = cScore; } public double getDsScore() { return dsScore; } public void setDsScore(double dsScore) { this.dsScore = dsScore; } } ``` 然后,我们定义一个`Main`类来保存十个学生对象,并按照Java、C语言、数据结构和总成绩进行排序: ```java import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Main { public static void main(String[] args) { // 创建十个学生对象 ArrayList<Student> students = new ArrayList<>(); students.add(new Student("001", "张三", 80, 70, 90)); students.add(new Student("002", "李四", 90, 80, 80)); students.add(new Student("003", "王五", 70, 90, 75)); students.add(new Student("004", "赵六", 85, 75, 85)); students.add(new Student("005", "钱七", 95, 85, 95)); students.add(new Student("006", "周八", 75, 95, 80)); students.add(new Student("007", "吴九", 85, 80, 90)); students.add(new Student("008", "郑十", 80, 85, 75)); students.add(new Student("009", "孙十一", 90, 90, 85)); students.add(new Student("010", "黄十二", 70, 75, 95)); // 按照Java成绩排序 Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return (int) (o2.getJavaScore() - o1.getJavaScore()); } }); System.out.println("按照Java成绩排序:"); for (Student student : students) { System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getJavaScore() + "\t" + student.getcScore() + "\t" + student.getDsScore()); } System.out.println(); // 按照C语言成绩排序 Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return (int) (o2.getcScore() - o1.getcScore()); } }); System.out.println("按照C语言成绩排序:"); for (Student student : students) { System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getJavaScore() + "\t" + student.getcScore() + "\t" + student.getDsScore()); } System.out.println(); // 按照数据结构成绩排序 Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return (int) (o2.getDsScore() - o1.getDsScore()); } }); System.out.println("按照数据结构成绩排序:"); for (Student student : students) { System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getJavaScore() + "\t" + student.getcScore() + "\t" + student.getDsScore()); } System.out.println(); // 按照总成绩排序 Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { double totalScore1 = o1.getJavaScore() + o1.getcScore() + o1.getDsScore(); double totalScore2 = o2.getJavaScore() + o2.getcScore() + o2.getDsScore(); return (int) (totalScore2 - totalScore1); } }); System.out.println("按照总成绩排序:"); for (Student student : students) { double totalScore = student.getJavaScore() + student.getcScore() + student.getDsScore(); System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getJavaScore() + "\t" + student.getcScore() + "\t" + student.getDsScore() + "\t" + totalScore); } System.out.println(); } } ``` 运行这个程序,就可以按照不同的排序方式输出学生信息了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值