按要求编写Java应用程序。
(1)创建一个叫做People的类:
属性:姓名、年龄、性别、身高
行为:说话、计算加法、改名
编写能为所有属性赋值的构造方法;
(2)创建主类:
创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80;
让该对象调用成员方法:
说出“你好!”
计算23+45的值
将名字改为“李四”
package lianxiti; public class People { private String name; private int age; private String xingbie; private double shengao; 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; } public String getXingbie() { return xingbie; } public void setXingbie(String xingbie) { this.xingbie = xingbie; } public double getShengao() { return shengao; } public void setShengao(double shengao) { this.shengao = shengao; } People(String name,int age,String xingbie,double shengao) { this.name=name; this.age=age; this.xingbie=xingbie; this.shengao=shengao; System.out.println("姓名是:"+this.name+" "+"年龄是:"+this.age+" "+"性别是:"+ this.xingbie+" "+"身高是:"+this.shengao+"厘米"); } void ShuoHua() { System.out.println("你好"); } int JiSuanJiaFa(int a,int b) { return a+b; } void GaiMing() { } }
package lianxiti; public class p { public static void main(String[] args) { People p1 = new People("张三",18,"男",180.0); p1.ShuoHua(); p1.JiSuanJiaFa(23, 45); System.out.println("23+45="+p1.JiSuanJiaFa(23, 45)); p1.setName("李四"); System.out.println(p1.getName()); p1.setXingbie("女"); System.out.println(p1.getName()+"的性别是"+p1.getXingbie()); } }