package com.oop.demo01;
public class Student {
//属性
String name;
int age;
//方法
public void study(){
System.out.println(this.name + "学生在学习");
}
//无参构造
public Student() {
}
//有参构造
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
package com.oop.demo01;
public class Application {
public static void main(String[] args) {
Student wu = new Student();
wu.name = "wu";
wu.study();
Student liu = new Student("liu",5);
liu.study();
}
}
本文介绍了一个使用Java实现的面向对象编程(OOP)示例:学生类的定义与使用。通过创建无参数与有参数构造函数,定义了学生类的属性如姓名和年龄,并实现了study()方法。在主函数中,实例化了两个学生对象并调用了study()方法进行展示。
1410

被折叠的 条评论
为什么被折叠?



