PTA习题 7-1 定义类与创建对象

定义一个类Person,定义name和age属性,定义有参的构造方法对name和age进行初始化。在测试类中创建该类的2个对象,姓名、年龄分别为lili、19和lucy、20,在屏幕打印出2个对象的姓名和年龄。

输入格式:

本题目无输入

输出格式:

在一行中输出一个人的姓名和年龄

输入样例:

在这里给出一组输入。例如:

 

输出样例:

在这里给出相应的输出。例如:

this person is lili,her age is 19
this person is lucy,her age is 20

代码长度限制16 KB

时间限制400 ms

内存限制64 MB

import java.util.*;
class Person{
    String name;
    int age;
}//类的定义格式:
//class 类名{
//  属性和方法 
//}
public class Main{
    public static void main(String args[]){
        Person a = new Person();
        Person b = new Person();
        a.name = "lili";
        a.age = 19;
        b.name = "lucy";
        b.age = 20;
        System.out.println("this person is "+a.name+",her age is "+a.age);
        System.out.println("this person is "+b.name+",her age is "+b.age);
      //  System.out.println("this person is lili,her age is 19");
       // System.out.println("this person is lucy,her age is 20");// 偷懒写法
    }
}

你可能感兴趣的:(PTA习题,PTA,PTA习题解答,java)