class Student{ private String name; private int age; private double cj; public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } public void setCj(double cj){ this.cj = cj; } public double getCj(){ return cj; } } public class Test{ public static void main(String args[]){ Student s = new Student(); s.setName("菠萝吹雪"); s.setAge(20); s.setCj(100); System.out.println(s.getName()); System.out.println(s.getAge()); System.out.println(s.getCj()); } }
时间: 2024-02-14 17:21:01 浏览: 182
这段代码定义了一个名为Student的类,并在Test类中实例化了该类并设置其属性值,然后通过调用get方法来获取属性值并打印输出。类中的三个属性分别为name(字符串类型)、age(整型)和cj(浮点型),并且都被设置为private,即只能在类的内部访问。因此,类中提供了三个对外开放的public方法,即setName、getName、setAge、getAge、setCj和getCj,用于设置和获取属性值。这样做的好处是可以对属性值进行严格的控制和检查,同时也可以保证属性值的安全性和隐私性。
与其他Java类相比,这段代码的特点在于它使用了封装的思想,即将属性设置为private,同时提供了对外开放的public方法,以实现对属性的访问和修改。这样做的好处在于可以避免属性值被误修改或篡改,从而保证了程序的稳定性和安全性。
相关问题
class Student{ private String name; private int age; private double cj; public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } public void setCj(double cj){ this.cj = cj; } public double getCj(){ return cj; } public class Test{ public static void main(String args[]){ Student s = new Student(); s.setName("菠萝吹雪"); s.setAge(20); s.setCj(100); System.out.println(s.getName()); System.out.println(s.getAge()); System.out.println(s.getCj()); } } }
这段代码与前面的代码相比,只有一个小错误,即没有正确关闭Student类的大括号。应该将Test类的大括号放在Student类的后面,如下所示:
```java
class Student{
private String name;
private int age;
private double cj;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void setCj(double cj){
this.cj = cj;
}
public double getCj(){
return cj;
}
}
public class Test{
public static void main(String args[]){
Student s = new Student();
s.setName("菠萝吹雪");
s.setAge(20);
s.setCj(100);
System.out.println(s.getName());
System.out.println(s.getAge());
System.out.println(s.getCj());
}
}
```
除此之外,这两段代码没有任何区别。
1. 设计Person类, (1)包含姓名(String)、性别(String)和年龄(int)成员变量,以及相应的getter和setter方法。 (2) 至少实现一个构造方法 (3)重写toString方法,输出Person对象信息; 2. 以Person为父类,设计一个学生类Student, (1)增加学号(int)、班级(String)、英语成绩、数学成绩和java成绩 这几个成员变量,以相应的getter和setter方法。 (2) 至少实现一个构造方法 (3) 重写toString()方法,输出Student对象信息。
1. Person类代码:
```java
public class Person {
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "姓名:" + name + ",性别:" + gender + ",年龄:" + age;
}
}
```
2. 学生类Student代码:
```java
public class Student extends Person {
private int studentNumber; // 学号
private String className; // 班级
private double englishScore; // 英语成绩
private double mathScore; // 数学成绩
private double javaScore; // Java成绩
public Student(String name, String gender, int age, int studentNumber, String className,
double englishScore, double mathScore, double javaScore) {
super(name, gender, age);
this.studentNumber = studentNumber;
this.className = className;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.javaScore = javaScore;
}
public int getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(int studentNumber) {
this.studentNumber = studentNumber;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getJavaScore() {
return javaScore;
}
public void setJavaScore(double javaScore) {
this.javaScore = javaScore;
}
@Override
public String toString() {
return super.toString() + ",学号:" + studentNumber + ",班级:" + className +
",英语成绩:" + englishScore + ",数学成绩:" + mathScore + ",Java成绩:" + javaScore;
}
}
```
阅读全文