day8
1、声明
package com.hg.hell;public class Student {
private String name;
private int age;
public Student() { }
public Student(String name) {
super();
this.name =name; }
public Student(String name,int age) {
super();
this.name =name;
this.age =age; }
public String getName() {
return name;
}
public void setName(String name) {
this.name =name; }
public int getAge()
{
return age; }
public void setAge(int age) {
this.age =age; }
}
2、代码
package com.hg.edgm;
public class demo01Student {
public static void main (String[] args) {
Student stu1=new Student();
stu1.show();
System.out.println("=============");
Student stu2=new Student("丫头",17);
stu2.show();
stu2.setAge(17);
stu2.show();
}
}
3、声明
package com.hg.edgm;public class Student {
private String name;
private int age;
public Student() {
System.out.println(“我是无参构造方法”);
}
public Student (String name,int age) {
System.out.println(“我是有参构造方法”); this.name=name;
if(age>=0 && age<=150)
this.age=age;
else
System.out.println(“age参数不合法”);
}
public void setName(String name) {
this.name=name;
}
public String getName() {
return this.name; }
public void setAge(int age)
{
this.age=age; }
public int getAge()
{
return this.age;
}
public void show() { System.out.println(“我叫:”+this.name+" "+this.age+“岁”); }
}
4、代码
package com.hg.hell;
public class demo01Student {
public static void main(String[] args) {
Student stu1= new Student(“密码”);
stu1.setAge(35);
System.out.println(“我是”+stu1.getName()+“我已经”+stu1.getAge()+“岁”);
System.out.println("=========");
Student stu2=new Student("小志",19);
System.out.println("我是"+stu2.getName()+"我已经"+stu2.getAge()+"岁");
stu2.setAge(19);
System.out.println("我是"+stu2.getName()+"我已经"+stu2.getAge()+"岁");
}
}