按要求编写Java应用程序。
(1)创建一个叫做People的类:
属性:姓名、年龄、性别、身高
行为:说话、计算加法、改名
编写能为所有属性赋值的构造方法;
(2)创建主类:
创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80;
让该对象调用成员方法:
说出“你好!”
计算23+45的值
将名字改为“李四”
private String name; private int age; private String sex; private double height; 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 getSex() { return sex; } public void setSex(String string) { this.sex = string; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } void speak() { System.out.println("哇啦哇啦哇啦"); } void addition(int a,int b) { System.out.println(a+"+"+b+"="+(a+b)); } void changeName() { System.out.print(name+"改名为"); name="李四"; System.out.print(name); } public static void main(String[] args) { People p1=new People(); p1.setName("张三"); p1.setSex("男"); p1.setAge(23); p1.setHeight(175); System.out.println("基本信息"+" "+"姓名:"+p1.getName()+" 性别:"+p1.getSex()+" 年龄:"+p1.getAge()+" 身高:"+p1.getHeight()); p1.speak(); p1.addition(23, 45); p1.changeName(); }
运行结果