代码
提交代码
public class ImoocStudent {
// 定义属性(特征)
String nickname; // 昵称
String position; // 职位
String city; // 城市
String sex; // 男 | 女
// 无参构造方法
public ImoocStudent() {
// 执行输出语句
System.out.println("无参构造方法执行了...");
}
// 有参构造方法
public ImoocStudent(String nickname, String position) {
// 将参数变量赋值给实例变量
this.nickname = nickname;
this.position = position;
}
// 有参构造方法
public ImoocStudent(String nickname, String position, String city, String sex) {
this.nickname = nickname;
this.position = position;
this.city = city;
this.sex = sex;
}
// 定义方法(行为)
public void studyCourse() {
System.out.println("学习课程");
}
public void postComment() {
System.out.println("发表评论");
}
public void postArticle() {
System.out.println("发表手记");
}
public static void main(String[] args) {
// 实例化学生对象
ImoocStudent student = new ImoocStudent();
// 给成员属性赋值
student.nickname = "Colorful";
student.position = "服务端工程师";
student.city = "北京";
student.sex = "男";
// 调用成员属性
System.out.println("昵称:" + student.nickname);
System.out.println("职位:" + student.position);
System.out.println("城市:" + student.city);
System.out.println("性别:" + student.sex);
ImoocStudent student1 = new ImoocStudent("慕女神", "UI设计师");
System.out.println("昵称为:" + student1.nickname);
System.out.println("职位为:" + student1.position);
}
}
运行结果