A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

package com.itheima;


/*
*第7题:
* 声明类Person,包含2个成员变量:name、age。定义函数sayHello(),调用时输出:我叫***,今年***岁了。声明类Chinese继承Person
*/
public class Person {
private String name;//姓名
    String gender;//性别
    public int age;//年龄

/*
* 类成员的访问控制修饰符
* 1,private只在同一类中可以使用
* 2,无修饰符 在同一类,同一包下的类可以使用
* 3,protected 在同一类,同一包,其子类可以使用
* 4,public  在任何地方都可以使用
*/
/*
* 1,将属性私有化
* 2,设置get/set方法
* 3,设置必要的读取限制
*/



public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getGender() {
return gender;
}


public void setGender(String gender) {
   if(gender.equals("男")||gender.equals("女"))
this.gender = gender;
   else
    System.out.println("***性别不合法***");
}


public int getAge() {
return age;
}


public void setAge(int age) {
if(age<0||age>150){
System.out.println("***年龄不合法***");
return;
}
this.age = age;
}


//构造方法
public Person(){
this.name = "无名氏";
this.gender = "男";
this.age = 18;
}

public Person(String name,String gender,int age){
this.name = name;
this.gender = gender;
this.age = age;
}

//方法自我介绍
public void say(){
System.out.println("自我介绍一下\r\n姓名:"+this.name+"\r\n性别:"
+this.gender+"\r\n年龄:"+this.age+"岁");

}


}


2.
package com.itheima;


public class PersonTest {




public static void main(String[] args) {
Person hanbing=new Person();
/*hanbing.name="韩冰";
hanbing.age=200;
hanbing.gender="中性";*/

hanbing.setName("韩冰");
hanbing.setAge(200);
hanbing.setGender("中性");
   //hanbing.gender="男";
//hanbing.age=25;
//hanbing.age =25;
hanbing.say();


}


}

3.
package com.itheima2;
import com.itheima.Person;
public class PersonTest {



public static void main(String[] args) {
Person hanbing=new Person();
hanbing.say();
//hanbing.age =25;


}


}

5 个回复

倒序浏览
什么啊 这么多 可以优化吗
回复 使用道具 举报
实在是太菜了。。。。。虽然我也很蔡,但不至于到这个程度。。。。。。
回复 使用道具 举报
= =。好长
回复 使用道具 举报
看看。学习下
回复 使用道具 举报
学习了.......
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马