当前位置: 首页 > 工具软件 > 2people > 使用案例 >

JAVA|People类

皇甫俊雅
2023-12-01

People类

按要求编写Java应用程序。
(1)创建一个叫做People的类:
属性:姓名、年龄、性别、身高
行为:说话、计算加法、改名
编写能为所有属性赋值的构造方法;

(2)创建主类:
创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80;让该对象调用成员方法:
说出“你好!”
计算23+45的值
将名字改为“李四”

public class shiyan10 {
    public static void main(String[] arg){
        People people=new People("张三",18,"男",1.80);
        System.out.println("名字:"+people.name+"\t性别:"+people.sex+"\t年龄:"+people.age+"\t身高:"+people.height);
        people.shuo("你好!");
        people.suan(23,45);
        people.gai("李四");
    }
}
class People{
    String name;
    int age;
    String sex;
    double height;
    People(String pname,int page,String psex,double pheight){
        name=pname;
        age=page;
        sex=psex;
        height=pheight;
    }
    
    String shuo(String hua){
        System.out.println("说:"+hua);
        return hua;
    }
    
    int suan(int a,int b){
        System.out.println("计算结果:"+(a+b));
        return a+b;
    }
    
    String gai(String ming){
        System.out.println("改的名字:"+ming);
        return ming;
    }
}

 类似资料: