zoukankan      html  css  js  c++  java
  • Java面向对象编程 -7

    类关联结构
    引用传递是整个Java开发与设计过程之中最为重要的技术组成,对于引用传递也与我们实际生活密切相关。

    class Car{
    	private float value;
    	private String name;
    	private Person person;
    	public void setName(String name){
    		this.name = name;
    	}
    	public String getName(){
    		return this.name;
    	}
    	public void setValue(float value){
    		this.value = value;
    	}
    	public float getValue(){
    		return this.value;
    	}
    	public void setPerson(Person person){
    		this.person = person;
    	}
    	public Person getPerson(){
    		return this.person;
    	}
    	public String getInfo(){
    		return "name"+name+"value"+value;
    	}
    	public Car(String name,float value){
    		this.name = name;
    		this.value = value;
    	}
    }
    
    
    class Person{
    	private String name;
    	private int age;
    	private Car car;
    	
    	public void setName(String name){
    		this.name= name;
    	}
    	public String getName(){
    		return this.name;
    	}
    	public void setAge(int age){
    		this.age = age;
    	}
    	public int getAge(){
    		return this.age;
    	}
    	public void setCar(Car car){
    		this.car = car;
    	}
    	public Car getCar(){
    		return this.car;
    	}
    	public String getInfo(){
    		return "name"+name+"age"+age;
    	}
    	public Person(String name,int age){
    		this.name = name;
    		this.age = age;
    	}
    }
    
    
    public class name {	
    	public static void main(String args[]){
    		Person person = new Person("张三",18);
    		Car car = new Car("宾利", 20000000.00f);
    		person.setCar(car);
    		car.setPerson(person);
    		System.out.println(person.getCar().getInfo());//代码链
    		System.out.println(car.getPerson().getInfo());
    	}
    }
    
    

    Car和Person都可以明确地描述某一类群体,现在是对于群体的关系做出的一种设置 这就实现了一种基础的引用过程

  • 相关阅读:
    [LeetCode] Maximum Depth of Binary Tree
    C++11中常用的几个简写
    [LeetCode] Word Break
    [LeetCode] Linked List Cycle II
    sizeof
    string和整形数据之间的转换
    ASCII
    [LeetCode] Linked List Cycle
    机器学习总结-线性回归
    推荐系统初探
  • 原文地址:https://www.cnblogs.com/sakura579/p/12499821.html
Copyright © 2011-2022 走看看