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

9 篇文章 1 订阅
8 篇文章 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");// 偷懒写法
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值