1 /* 2 * 按要求编写Java应用程序。 3 * (1)创建一个叫做People的类: 属性:姓名、年龄、性别、身高 4 * 行为:说话、计算加法、改名 5 * 编写能为所有属性赋值的构造方法; 6 * (2)创建主类: 创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80; 7 * 让该对象调用成员方法: 8 * 说出“你好!” 计算23+45的值 将名字改为“李四” 9 */ 10 private String name; 11 private String sex; 12 private double height ; 13 private int age; 14 int speak; 15 int jisuan; 16 String changename; 17 18 public People(String name, String sex, double height, int age) { 19 super(); 20 this.name = name; 21 this.sex = sex; 22 this.height = height; 23 this.age = age; 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public String getSex() { 35 return sex; 36 } 37 38 public void setSex(String sex) { 39 this.sex = sex; 40 } 41 42 public double getHeight() { 43 return height; 44 } 45 46 public void setHeight(double height) { 47 this.height = height; 48 } 49 50 public int getAge() { 51 return age; 52 } 53 54 public void setAge(int age) { 55 this.age = age; 56 } 57 58 public void shouName() 59 { 60 System.out.println("姓名:"+name); 61 } 62 public void shouSex() 63 { 64 System.out.println("性别:"+age); 65 } 66 public void shouHeight() 67 { 68 System.out.println("身高:"+height); 69 } 70 public void shouAge() 71 { 72 System.out.println("年龄:"+age); 73 } 74 public String shouSpeak(String speak) 75 { 76 System.out.println("shouspeak的内容为:"+speak); 77 return speak; 78 } 79 public int jisuan(int a,int b,int c) 80 { 81 c=a+b; 82 System.out.println("计算结果为:"+c); 83 return jisuan; 84 } 85 public String changename(String name) 86 { 87 System.out.println("修改的名字为:"+name); 88 return changename; 89 } 90 public static void main(String[] args) { 91 /* (2)创建主类: 创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80; 92 * 让该对象调用成员方法: 93 * 说出“你好!” 计算23+45的值 将名字改为“李四” 94 */ 95 People people=new People("张三","男",1.80,18); 96 people.shouName(); 97 people.shouSex(); 98 people.shouHeight(); 99 people.shouAge(); 100 people.shouSpeak("你好"); 101 people.jisuan(1,2,5); 102 people.changename("李四"); 103 104 }
时间: 2024-10-25 06:17:43