Unified Modeling Language tool introduced: powerdesigner
file———>new model——>model Type——>object oriented model——>class digram
FIG relationship classes are:
① represents the generalization of inheritance:
② represents the realization of the relationship is to achieve
③ represents dependencies dependent
For example in this case is the relationship between the TV and the remote control
④ relationship (1V1,1VN, NVN)
Express this relationship are: customers and orders, orders and commodities
⑤ aggregation relationship
Examples of this are: players, teams, doctor
⑥ combination relationship
This is a strong dependence, for example the players, head, legs, feet
1, the inheritance relationship between classes and the classes are the extends keyword, in particular is a UML diagram is a generalization relationship is
① parent
package com.wyq.study;
public class Father{//书写类
//书写属性
private String name;
private int age;
//提供共有的取值赋值方法
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
if(age<0||age>100){
System.out.println("年龄输入有误,请重新输入。");
this.age = 1;
}else{
this.age = age;
}
}
public int getAge(){
return age;
}
//书写无参构造方法
public Father(){
System.out.println("这里是Father的无参的构造方法");
}
public Father(String name,int age){
this.name =name;
this.setAge(age);
System.out.println("这里是Father的带参的构造方法,参数分别是:"+this.name+"\t"+this.getAge());
}
//书写普通方法
public void speak(String language,String work){
System.out.println("这里是父类Father普通方法的测试:其参数为:"+language+"\t"+work+"\t"+this.name+"\t"+this.getAge());
}
}
② subclass
package com.wyq.study;
public class Son extends Father{
//属性
private String schoolName;
private int clazz;
//属性私有化
public void setSchoolName(String schoolName){
this.schoolName = schoolName;
}
public String getSchoolName(){
return schoolName;
}
public void setClazz(int clazz){
if(clazz<0||clazz>10){
System.out.println("班级的输入有误。");
this.clazz = 5;
}else
this.clazz = clazz;
}
public int getClazz(){
return clazz;
}
//无参构造
public Son(){
super();
System.out.println("这里是son类的无参构造");
}
public Son(String name,int age,String schoolName,int clazz){
super( name, age);
this.schoolName = schoolName;
this.setClazz(clazz);
System.out.println("这里是son类的带参构造"+"\t"+super.getName()+"\t"+super.getAge()+"\t"+this.getSchoolName()+"\t"+this.schoolName+"\t"+this.getClazz());
}
//书写普通方法
public void study(String course){
System.out.println("正在学习"+course+"\t"+this.schoolName+"\t"+this.getClazz());
}
@Override
public void speak(String language, String work) {
super.speak(language, work);
System.out.println("这里是重写子类Son的测试,参数为:"+super.getName()+"\t"+super.getAge()+"\t"+this.getSchoolName()+"\t"+this.getClazz());
}
}
③ test class
package com.wyq.study;
import javax.swing.DebugGraphics;
public class TestSon {
public static void main(String[] args) {
Son s = new Son();//调用子类的无参构造
Son so = new Son("张三",20,"北京大学",2);
so.study("计算机");
System.out.println(so.getName()+"\t"+so.getAge()+"\t"+so.getSchoolName()+"\t"+so.getClazz());
so.speak("python", "码农");
System.out.println("************************");
// Debug;
Father f = new Father("王五",-10);
f.speak("C语言", "工人");
}
}
2, to achieve relationships: class implements an interface that has a relationship
① Interface
package com.wyq.study;
public interface InterF {
public static final String Name = "张三";
public void fun(String grade);
}
② implementation of the interface class
package com.wyq.study;
public class ImplImp implements InterF{
@Override
public void fun(String grade) {
System.out.println("我今年读"+grade);
}
}
③ test class
package com.wyq.study;
public class TestIm {
public static void main(String[] args) {
ImplImp im = new ImplImp();
im.fun("三年级");
System.out.println(im.Name);
}
}
3, the dependency: a class using another class
For example a television set and a remote control
① the original class
package com.wyq.study;
public class TV {
public void open(){
}
public void close(){
}
public void change(){
}
}
② being dependent classes
package com.wyq.study;
public class Control {
public void open(TV tv){
tv.open();
}
public void close(){
TV tv = new TV();
tv.close();
}
}
③ test class
package com.wyq.study;
public class TestTV {
public static void main(String[] args) {
TV tv = new TV();
Control ct = new Control();
ct.open(tv);
}
}
4, relationship
There are one to one, one to many, many to many
Examples are customers and orders, orders and commodities
Relationship is long-term, strong, reflected in the code is a Class B Class A member variables do
商品类
public class Product {
}
订单类 --》订单中可以有N多商品
public class Order {
// 在订单中有 N 多商品
// 属性
Product [] pros = new Product[1000];
}
客户--》可以下订单
public class Customer {
private Order order ;
}
5, the relationship between the polymerization
This relationship is similar: the team, the players, the team doctor, coach
public class Player { // 球员
}
public class Coach { // 教练
}
public class Doctor { // 队医
}
public class Team {
// 私有属性
private Player p ; // 球员
private Coach c ; // 教练
private Doctor d ; // 队医
}
6, the combination of relationship
Composition relationship is part of the overall, a player for example, the head, legs and other
public class Head { // 头
}
public class Leg {
}
public class Player { // 球员
private Head head ;
private Leg leg ;
}
8, summary
(1)extends
(2)implement
Parameter (3) a method for the class / local variable -> dependent
(4) A method for a class member variables -> (association, aggregation, composition)