Java代码题: 在项目下新建一个名为com和xsc的两个包,在com包下创建一个名为Student的公共类,该类中含有四个不同类型的属性和方法,在其主方法中,通过该类对象调用该类所有属性和方法。
时间: 2023-06-16 15:08:14 浏览: 48
下面是Java代码实现:
```java
package com;
public class Student {
public String name;
public int age;
public char gender;
public double score;
public void study() {
System.out.println("I'm studying");
}
public void eat() {
System.out.println("I'm eating");
}
public void sleep() {
System.out.println("I'm sleeping");
}
public void showInfo() {
System.out.println("My name is " + name + ", I'm " + age + " years old, " + gender + ", and my score is " + score);
}
public static void main(String[] args) {
Student student = new Student();
student.name = "Tom";
student.age = 18;
student.gender = 'M';
student.score = 90.5;
student.study();
student.eat();
student.sleep();
student.showInfo();
}
}
```
在该代码中,我们定义了一个公共的 `Student` 类,在其中包含了四个不同类型的属性 `name`、`age`、`gender`、`score` 和三个方法 `study()`、`eat()`、`sleep()`,以及一个展示信息的方法 `showInfo()`。在主方法中,我们通过 `Student` 类对象 `student` 调用了该类的所有属性和方法。