请老师看下这样可以吗

请老师看下这样可以吗

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回答

同学你好,

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、这里不需要指点默认值,这样就不符合面向对象的逻辑了

http://img1.sycdn.imooc.com//climg/5e99521609bf095e09160515.jpg

建议同学可以先看完整个老师的案例,跟着老师敲一下代码哦,等看完整个案例,跟着老师敲一遍代码,再回过头来看这个案例,同学会更清晰一些。

祝学习愉快。


  • 慕UI5411124 提问者 #1
    好的 谢谢老师
    2020-04-17 15:22:09
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师