Java面向对象:Student类

描述
【描述】
定义一个Student类,表示学生信息。Student类有学号(id,整型)、姓名(name,字符串)、性别(sex,字符型,m表示男性,f表示女性)三个私有数据域;有参构造方法将学号、姓名、性别设置为给定的参数;成员方法display显示学生的信息。

注意,Student类的定义应该这样开始:

class Student {

也就是说,Student类的class前面不要有public。

【输入】
输入学号、姓名、性别。

【输出】
输出学号、姓名、性别。

【输入示例】
1000

Tan

m

【输出示例】
1000

Tan

m`

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int id=input.nextInt();
        String s=new String();
        s=input.next();
        char c=input.next().charAt(0);
        input.close();
        Student st=new Student(id,s,c);
        st.display();
    }
}

class Student {
    private int id;
    String name=new String();
    private char sex;
    public  Student(int a,String b,char c)
    {
        id=a;
        name=b;
        sex=c;
    }
    public void display()
    {
        System.out.println(id);
        System.out.println(name);
        System.out.println(sex);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值