创建学员类
package com.oned6z;
/**
 * @program: com.oned6z
 * @description: 创建学员类
 * @author: Mr.Lin
 * @create: 2019年7月9日
 **/
public class Stu {
	public String name;
	public int age;
	public String cla;
	public String hobby;
	
	public String xiangguanXinxi(){
		String xinxi = name+"\n年龄:"+age+"\n就读于:"+cla+"\n爱好:"+hobby;
		return xinxi;
		
	}
}
package com.oned6z;
/**
 * @program: com.oned6z
 * @description: 创建学员的对象
 * @author: Mr.Lin
 * @create: 2019年7月9日
 **/
public class TestStu {
	public static void main(String[] args) {
		//创建对象: 类名 对象名 = new 类名();
		Stu stu1 = new Stu();
		//使用对象的属性和方法:      对象名.属性      对象名.方法名()
		stu1.name = "张浩";
		stu1.age = 10;
		stu1.cla = "S1班";
		stu1.hobby = "篮球";
		
		String s = stu1.xiangguanXinxi();
		System.out.println(s);
		
	}

创建教员类
package com.oned6z;
/**
 * @program: com.oned6z
 * @description: 创建教员类
 * @author: Mr.Lin
 * @create: 2019年7月9日
 **/
public class Tec {
	public String name;
	public String major;
	public String subject;
	public int teachAge;
	
	public String xiangguanXinxi() {
		String xinxi = name+"\n专业方向:"+major+"\n教授课程:"+subject+"\n教龄:"+teachAge;
		return xinxi;
	}
}
package com.oned6z;
/**
 * @program: com.oned6z
 * @description: 创建教员的对象
 * @author: Mr.Lin
 * @create: 2019年7月9日
 **/
public class TestTec {
	public static void main(String[] args) {
		Tec tec1 = new Tec();
		tec1.name = "王老师";
		tec1.major = "计算机";
		tec1.subject = "使用Java语言理解程序逻辑";
	    tec1.teachAge = 5;
	    
	    String s = tec1.xiangguanXinxi();
	    System.out.println(s);
	}
}

原文:https://www.cnblogs.com/lpbk/p/11156406.html