博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
定义一个简单的teacher类
阅读量:3961 次
发布时间:2019-05-24

本文共 659 字,大约阅读时间需要 2 分钟。

定义一个teacher类,包括名字,年龄,学历,职位与自我介绍的方法

如图所示:

在这里插入图片描述

运行的结果
在这里插入图片描述
对于一些对象的值是有规定的就可以使用private私有再对其进行封装,比如性别只有男与女,就会使用到private与封装
如下

代码如下

package teacher;public class teacher {	public String name;	public int age;	public String edcation;	public String position;	public teacher(String name, int age, String edcation, String position) {		this.age = age;		this.name =name;		this.edcation =edcation;		this.position=position;	}	public String introduction() {		return "大家好!,我是"+name+",我今年"+age+"岁了,我的学历是"+edcation+",我的职位是"+position;	}	static class test {		public static void main(String[] args) {			teacher th =new teacher ("李华",41,"本科","");			System.out.println(th.introduction());		}	}}

转载地址:http://quqzi.baihongyu.com/

你可能感兴趣的文章