请老师看下这样可以吗
package com.imooc.imooCase;
public class cpProj {
//设置私有属性 姓名,学号,性别,年龄,科目,年限--------------------
private String stuName;
private String stuNo;
private char sexual;
private int age;
private String proj="计算机科学应用";
private String projNo="J0001";
private int years=4;
//静态构造块输出-----------------------------------------
static {
cpProj one=new cpProj();
System.out.println("专业信息如下:");
System.out.println("专业名称:"+one.getProj());
System.out.println("专业编号:"+one.getProjNo());
System.out.println("学制年限:"+one.getYears());
System.out.println("==============");
}
//设置属性get/set方法-----------------------------------
//姓名=============================
public void setStuName(String stuName) {
this.stuName=stuName;
}
public String getStuName() {
return this.stuName;
}
//学号=============================
public void setStuNo(String stuNo) {
this.stuNo=stuNo;
}
public String getStuNo() {
return this.stuNo;
}
//性别=============================
public void setSexual(char sexual) {
this.sexual=sexual;
}
public char getSexual() {
return this.sexual;
}
//年龄=============================
public void setAge(int age) {
this.age=age;
}
public int getAge() {
return this.age;
}
//设置专业科目,专业编号,学制年限为只读
public String getProj() {
return this.proj;
}
public String getProjNo() {
return this.projNo;
}
public int getYears() {
return this.years;
}
//构造方法---------------------------------------------
public cpProj() {
}
public cpProj(String stuName,String stuNo,char sexual,int age) {
setStuName(stuName);
setStuNo(stuNo);
setSexual(sexual);
setAge(age);
}
}
================================================================
package com.imooc.imooCase;
public class showStuInformation {
public void show(cpProj one) {
System.out.println("学生信息如下:");
System.out.println("姓名:"+one.getStuName());
System.out.println("学号:"+one.getStuNo());
System.out.println("性别:"+one.getSexual());
System.out.println("年龄"+one.getAge());
System.out.println("所报专业名称:"+one.getProj());
System.out.println("学制年限:"+one.getYears());
System.out.println("==============");
}
}
================================================================
package com.imooc.imooCase;
public class cpProjTest {
public static void main(String[]args) {
//带参构造张三李四
cpProj one =new cpProj("张三","S01",'男',18);
cpProj two=new cpProj("李四","S02",'女',17);
//无参构造王五
cpProj three=new cpProj();
//set方法给王五属性赋值
three.setStuName("王五");
three.setStuNo("S03");
three.setSexual('男');
three.setAge(18);
showStuInformation sw=new showStuInformation();
sw.show(one);
sw.show(two);
sw.show(three);
}
}
=================================================================
专业信息如下:
专业名称:计算机科学应用
专业编号:J0001
学制年限:4
==============
学生信息如下:
姓名:张三
学号:S01
性别:男
年龄18
所报专业名称:计算机科学应用
学制年限:4
==============
学生信息如下:
姓名:李四
学号:S02
性别:女
年龄17
所报专业名称:计算机科学应用
学制年限:4
==============
学生信息如下:
姓名:王五
学号:S03
性别:男
年龄18
所报专业名称:计算机科学应用
学制年限:4
==============
正在回答
同学你好,
1、首先,关于代码规范,类名首字母要大写:
2、这里不用用静态代码块儿输出,
//静态构造块输出-----------------------------------------
static {
cpProj one=new cpProj();
System.out.println("专业信息如下:");
System.out.println("专业名称:"+one.getProj());
System.out.println("专业编号:"+one.getProjNo());
System.out.println("学制年限:"+one.getYears());
System.out.println("==============");
}
3、这里不需要指点默认值,这样就不符合面向对象的逻辑了

建议同学可以先看完整个老师的案例,跟着老师敲一下代码哦,等看完整个案例,跟着老师敲一遍代码,再回过头来看这个案例,同学会更清晰一些。
祝学习愉快。
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星