一、简单JAVA类练习
class Dog{
private String name;
private String color;
private int age;
public Dog(){}
public Dog(String name,String color,int age){
this.name = name;
this.color = color;
this.age = age;
}
public String getInfo(){
return "名字"+this.name+"\n"+"、颜色"+this.color+"\n"+"、年龄"+this.age;
}
}
public class Demo11Test {
public static void main(String agrs[]){
Dog dog = new Dog("嘿嘿","灰色",1);
System.out.println(dog.getInfo());
}
}
二、数据表与简单JAVA(一对多)
假设有两张数据表:emp、dept:
- emp表: empno、ename、job、sal、comm、mgr、deptno;
- dept表:deptno、dname、loc。
要求可以通过程序描述出如下的对应关系:
- 一个部门有多个雇员,并且可以输出一个部门的完整信息(包括雇员信息);
- 可以根据一个雇员找到雇员对应的领导信息和雇员所在部门的信息
class Emp {
private int empno;
private String ename;
private String job;
private double sal;
private double comm;
private Emp mgr; //描述雇员领导
private Dept dept; //描述雇员所在部门
public Emp() {}
public Emp(int empno, String ename, String job, double sal, double comm) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.sal = sal;
this.comm = comm;
}
public void setMgr(Emp mgr) {
this.mgr = mgr;
}
public Emp getMgr() {
return this.mgr;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Dept getDept() {
return this.dept;
}
public String getInfo() {
return "【EMP】empno = " + this.empno + "ename = " + this.ename + "job = " + this.job + "sal = " + this.sal + "comm = " + this.comm;
}
}
class Dept {
private int deptno;
private String dname;
private String loc;
private Emp[] emps; //所有雇员
public Dept() {}
public Dept(int deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public void setEmps(Emp[] emps) {
this.emps = emps;
}
public Emp[] getEmps() {
return this.emps;
}
public String getInfo() {
return "deptno = " + this.deptno + ", dname = " + this.dname + ", loc = " + this.loc;
}
}
public class TestDemo {
public static void main(String[] args) {
// 第一步:设置类对象的关系
// 1. 分别创建各自类的实例化关系
Dept dept = new Dept(10, "ACCOUNTING", "NEW YORK");
Emp ea = new Emp(7369, "SMITH", "CLERK", 800.0, 0.0);
Emp eb = new Emp(7543, "ALLEN", "MANAGER", 2456.0, 0.0);
Emp ec = new Emp(7893, "KING", "PRESIDENT", 5000.0, 0.0);
// 2. 设置雇员领导的关系
ea.setMgr(eb);
eb.setMgr(ec); //ec没有领导
// 3. 设置雇员和部门的关系
ea.setDept(dept);
eb.setDept(dept);
ec.setDept(dept);
// 4. 设置雇员和部门的关系
dept.setEmps(new Emp[] {ea, eb, ec});
//第二部:进行数据的取得
// 5. 一个部门有多个雇员,并且可以输出一个部门的完整信息(包括雇员信息)
System.out.println(dept.getInfo()); // 输出部门所有信息
for(int x = 0; x < dept.getEmps().length; x++) {
System.out.println("\t" + dept.getEmps()[x].getInfo());
if(dept.getEmps()[x].getMgr() != null) //判断有领导
System.out.println("\t\t" + dept.getEmps()[x].getMgr().getInfo());
}
System.out.println("==========================");
//6. 根据一个雇员找到雇员对应的领导信息和雇员所在部门的信息
System.out.println(eb.getInfo());
if(eb.getMgr() != null) { //有领导
System.out.println("\t" + eb.getMgr().getInfo());
}
if(eb.getDept() != null) {
System.out.println("\t" + eb.getDept().getInfo());
}
}
}
三、数据表与简单Java类(多对多)
1、定义一个学生选课的操作表:三张数据表
- 学生表:学生编号、姓名、年龄;
- 课程表:课程编号、课程名称、学分;
- 学生-课程关系表:学生编号、课程编号、成绩。
要求:可以实现如下的信息输出:
- 可以找到一门课程,以及参加此课程的所有的学生信息,和他的成绩
- 可以根据一个学生,找到所参加的所有课程和每门成绩。
代码如下:
class Student{
private int stuid ;
private String name ;
private int age ;
private Course[] courses ;
private StudentCourse[] studentCourses ;
public Student(){}
public Student(int stuid,String name,int age){
this.stuid = stuid ;
this.name = name ;
this.age = age ;
}
public void setStudentCourses(StudentCourse[] studentCourses) {
this.studentCourses = studentCourses;
}
public StudentCourse[] getStudentCourses() {
return this.studentCourses;
}
public void setCourses(Course[] courses) {
this.courses = courses;
}
public Course[] getCourses() {
return this.courses;
}
public String getInfo(){
return "学生编号:" + this.stuid + ",姓名:" + this.name + ",年龄:"+this.age;
}
}
class Course{
private int cid ;
private String name ;
private int credit ;
private StudentCourse[] studentCourses;
public Course(){}
public Course(int cid, String name, int credit){
this.cid = cid ;
this.name = name ;
this.credit = credit ;
}
public void setStudentCourses(StudentCourse[] studentCourses) {
this.studentCourses = studentCourses;
}
public StudentCourse[] getStudentCourses() {
return this.studentCourses;
}
public String getInfo(){
return "课程编号:"+ this.cid + ",课程名称:" + this.name + ",学分:"+this.credit ;
}
}
class StudentCourse{
private Student student ;
private Course course ;
private double score ;
public StudentCourse(){}
public StudentCourse(Student student,Course course,double score){
this.student = student ;
this.course = course ;
this.score = score ;
}
public Student getStudent() {
return this.student;
}
public Course getCourse() {
return this.course;
}
public double getScore() {
return this.score;
}
}
public class JavaTest {
public static void main(String args []){
//第一步:根据结构进行关系设置
//1.创建各自的独立的对象
Student stu1 = new Student(1,"李四",18);
Student stu2 = new Student(1,"张三",17);
Student stu3 = new Student(1,"王五",18);
Course ca = new Course(1001,"马克思",3);
Course cb = new Course(1002,"操作系统",2);
//2、需要设置学生和课程的关系,设置成绩
stu1.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu1,ca ,99.9),
new StudentCourse(stu1,cb , 87.1)
});
stu2.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu2,ca,79.9)
});
stu3.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu3,cb, 89.9)
});
//3、设置课程与学生的关系
ca.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu1,ca,99.9),
new StudentCourse(stu2,ca,79.9)
});
cb.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu1,ca,99.9),
new StudentCourse(stu3,cb,89.9)
});
//第二部:根据结构取出数据
//1、可以找到一门课程,以及参加此课程的所有学生信息和他的成绩
System.out.println(ca.getInfo());
for (int x = 0;x < ca.getStudentCourses().length;x++){
System.out.println("\t|-"+ca.getStudentCourses()[x].getStudent().getInfo());
System.out.println(",成绩:"+ca.getStudentCourses()[x].getScore());
System.out.println();
}
System.out.println("===============");
System.out.println(stu1.getInfo());
for (int x = 0;x < stu1.getStudentCourses().length;x++){
System.out.println("\t|-"+stu1.getStudentCourses()[x].getCourse().getInfo());
System.out.println(",成绩:"+stu1.getStudentCourses()[x].getScore());
System.out.println();
}
}
}
以上就是三个简单JAVA类练习