编写一个java程序,封装一个student类的对象,定义一个表示学生的类student

编写一个Java程序,在测试类中创建一个Student的对象,修改该对象的年龄,并输出该对象的年龄。~

public class Student { /** * @param sNo * @param sName * @param sSex * @param sAge * @param sGrade */ public Student(String sNo, String sName, String sSex, int sAge, String sGrade) { super(); this.sNo = sNo; this.sName = sName; this.sSex = sSex; this.sAge = sAge; this.sGrade = sGrade; } /** * @return the sAge */ public int getsAge() { return sAge; } /** * @param sAge the sAge to set */ public void setsAge(int sAge) { this.sAge = sAge; } /** * @return the sName */ public String getsName() { return sName; } /** * @return the sGrade */ public String getsGrade() { return sGrade; } private String sNo; private String sName; private String sSex; private int sAge; private String sGrade;}public class TestStudent { public static void main(String[] args) { Student student = new Student("001", "Tom", "male", 28, "grade 6"); System.out.println("Name = " + student.getsName() + ";Age = " + student.getsAge() + ";Grade = " + student.getsGrade()); student.getsAge(); student.getsGrade(); student.getsName(); student.setsAge(12); System.out.println("Age after modify = " + student.getsAge()); }}

1、新建一个272.php,如图所示:

2、输入php网页的结构(),如图所示:

3、声明PHP与浏览器交互的文件类型和编码,如图所示:

4、使用class关键字,定义一个Student类,代码如图所示:

5、给Student类添加成员变量和成员方法,代码如图所示:

6、给Student类,创建一个对象,代码:$s1=newStudent()。

public class Student{

    private String num;
    private String sex;
    private String name;
    private int age;
    
    public Student(String num,String sex,String name){
        this.num=num;
        this.sex=sex;
        this.name=name;
    }
    
    public String getNum(){
        return this.num;
    }
    
    public String getSex(){
        return this.sex;
    }
    
    public String getName(){
        return this.name;
    }
    
    public int getAge(){
        return this.age;
    }
    
    public void setAge(int age){
        this.age=age;
    }
    
    public String toString(){
        return "My name is %s, I'm %s years old. I'm a %s. My student num is %s";
    }
}

public class Test{
    public static void main(String[] args){
        Student student=new Student("N01","boy","zhangsan");
        student.setAge(18);
        System.out.priltln(String.format(student.toString(),student.getName(),student.getAge().toString(),student.getSex(),student.getNum()));
    }
}

ok啦  就是这样



用java编写一个名为Square(正方形)的类,并按要求完成如下封装
答:随便写写 未测试 package com.leejiliang.HomeworkDemo2;public class Test { public static void main(String[] args) { Square[] squares = new Square[10]; for (int i = 0, k = squares.length; i < k; i++) { squares[i] = new Square(i + 1); } Syst...

在java中编写程序,定义一个学生类
答:代码如下:package exam2;import java.util.ArrayList;import java.util.List;/ 编写一个Java应用程序,该程序包括3个类:定义一个学生类,Student有姓名,学号,选学的课程列表 定义一个课程类:课程名称,课程分数 (1)初始化一个学生,选择了三门课程,并添加到学生的属性中 (2)实现统计学生的总分...

编写一个java程序
答:打印出个数最多的字母及个数:字母:a 个数:5 打印出所有字母及个数:字母:a 个数:5 字母:b 个数:2 字母:d 个数:2 字母:c 个数:3 字母:f 个数:2 字母:e 个数:2 字母:s 个数:1 字母:w 个数:1 字母:t 个数:2 字母:r 个...

跪谢!求用JAVA编写一个自动贩卖机的程序
答:import java.util.Scanner;public class Shop { private String coffee="咖啡";private String coke="可乐";private String tea="茶";private double coffeeprice=2.00;private double cokeprice=3.00;private double teaprice=5;private double price=0.0;public Shop(){ } public void getName(...

怎么用java写一个窗体程序?
答:首先写一个frame类,继承Frame,是继承widows然后把,出现窗口的语句封装成一个函数 publicvoidlunchFrame(){ this.setLocation(0,0);this.setSize(20,20);setVisible(True);//一定要写这句话 } 最后只需要在主函数里面调用就可以 Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃...

java程序,编写一个student的类,包含string name 和int age
答:class Student {String name;int age;String school;String department;public Student(String name, int age, String school, String department) {this.name = name;this.age = age;this.school = school;this.department = department;}public void printInfo() {System.out.println("姓名:" + ...

用JAVA编一个程序 输入10名同学的身高,找出最高升高,要求使用对象数组类 ...
答:import java.util.Scanner;public class TestStudent { public static void main(String[] args){ Scanner sc=new Scanner(System.in);Student[] stus=new Student[10];/ 将输入的是个学生的身高实例化是个学生,并保存到Student对象数组中 / for(int i=1;i<=10;i++){ System.out.println("...

编写一个Java应用程序,该程序中有2个类:Trangle、和Circle,分别用来描述...
答:代码如下:class Triangle {private double a;private double b;private double c;private double perimeter;// 周长private double area;// 面积public Triangle(double a, double b, double c) {this.a = a;this.b = b;this.c = c;this.perimeter = a + b + c;double p = (a + b ...

怎么用java写一个窗体程式?
答:怎么用java写一个tomcat的启动,停止程式 可以利用Runtime类,Runtime用于别是虚拟机器执行时的状态,它用于封装JVM虚拟机器程序。 看看,我给你写个程式码: public class Run { public static void main(String[] args) throws Exception { Runtime run=Runtime.getRuntime(); Process ...

使用JAVA编写一个简单的银行存取款程序
答:另一个构造方法带4个参数分别初始化4个属性(带数据有效性验证)。/ public Account(String id, int password, String name, double balence) { this.id = id;this.password = password;this.name = name;this.balence = balence;} / 查询余额 / public static double selectMoney(Account account...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网