创建一个Student类,其成员变量name(姓名),age(年龄),sex(性别)为私有变量,成员方法:say()、无参和带1个参、2个参和3个参的构造方法Student()
在主方法中使用new 创建四个Student对象stu1、stu2、stu3和 stu4;
通过调用无参构造方法实现对stu1三个属性赋值;
通过调用1个参的构造方法实现对stu2三个属性赋值;
通过调用2个参的构造方法实现对stu3三个属性赋值;
通过调用3个参的构造方法实现对stu4三个属性赋值;
调用stu1、stu2、stu3和 stu4的say()方法.
这个好难写了几次都是错误的
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
关注 public class Student { private String name; private int age; private String sex; public Student() {} public Student(String name) { this.name = name; } public Student(String name, int age) { this.name = name; this.age = age; } public Student(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } public void say(){ System.out.println("学生会说话"); } public static void main(String[] args) { Student stu1 = new Student(); Student stu2 = new Student("张三"); Student stu3 = new Student("李四",20); Student stu4 = new Student("王五",20,"男"); stu1.say(); stu2.say(); stu3.say(); stu4.say(); } }
运行结果:
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 merge函数占用内存过大
- ¥15 Revit2020下载问题
- ¥15 使用EMD去噪处理RML2016数据集时候的原理
- ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
- ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
- ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
- ¥15 如何在炒股软件中,爬到我想看的日k线
- ¥15 seatunnel 怎么配置Elasticsearch
- ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
- ¥15 (标签-MATLAB|关键词-多址)