package com.lxk.jsu;
//定义了一个名为MyGirlFriend的类
class MyGirlFriend {
//定义属性,成员变量
private String name;
private int age;
private String sex;
//有参构造方法
public MyGirlFriend(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
//说话方法
public void say() {
System.out.println(this.name+" :爱意随风起,风止意难平,遗憾终究是遗憾。");
}
}
主程序
package com.lxk.jsu;
//在主程序中使用MyGirlFriend类
public class RunDemo {
public static void main(String[] args){
//给自己一个野生的女朋友
MyGirlFriend myGirlFriend = new MyGirlFriend("流莹", 18, "女");
//让她说话
myGirlFriend.say();
}
}
输出:
E:\develop\Java\jdk-17\bin\java.exe "-javaagent:E:\idea\IntelliJ IDEA Community Edition 2023.3.4\lib\idea_rt.jar=55052:E:\idea\IntelliJ IDEA Community Edition 2023.3.4\bin" -Dfile.encoding=UTF-8 -classpath E:\code\stduy1\out\production\stduy1 com.lxk.jsu.RunDemo
流莹 :爱意随风起,风止意难平,遗憾终究是遗憾。
Process finished with exit code 0
作者本人也是初学,希望能和各位一同进步!