zoukankan      html  css  js  c++  java
  • JAVA基础学习之路(四)定义简单java类

    简单java类开发一般原则:

    1. 类名称必须有意义,再怎么说,要让人家看的明白吧
    2. 类之中所有属性必须使用private封装,并提供setter,getter方法
    3. 类之中可以有多个构造方法,但是必须保留有一个无参数构造方法
    4. 类之中不允许出现任何输出语句,所有输出必须交给被调用处
    5. 类之中需要有一个可以取得对象完整信息的方法,一般叫做getInfo(),返回String型数据
    class Book {
        private String name;
        private int price;
        private int num;
        public Book() {
            
        }
        public Book(String na ,int pri, int n) {
            name = na;
            price = pri;
            num = n;
        }
        public book() {
            
        }
        public void setName(String n) {
            name = n;
        }
        public void setPrice(int p) {
            price = p;
        }
        public void setNum(int n) {
            num = n;
        }
        public String getName() {
            return name;
        }
        public int getPrice() {
            return price;
        }
        public int getNum() {
            return num;
        }
        public  int  getMonney(int price, int num) {
            return price*num;
        }
        public String getinfo() {
            return "书名:"+name+"
    "+
                   "单价:"+price+"
    "+
                   "数量:"+num+"
    ";
        }
    }
    
    public class test2 {
        public static void main(String args[]) {
            Book monney = new Book("老人与海",20,3);
            System.out.println(monney.getinfo());
        }
    }

    一对多关系:

    实例1:

    省份:

    省份ID : NUMBER <pk>

    省份名字: VARCHAR(50)

    城市:

    城市ID  : NUMBER<pk>

    省份ID:NUMBER<fk>

    城市名称:VARCHAR(50)

    要求可以通过一个省份找到所有的城市信息,通过一个城市可以找到它对应的省份信息

    //setter,getter,无参构造省略
    class Province {
        private int pid;
        private String pname;
        private City city [];
        public Province(int pid, String pname) {
            this.pid = pid;
            this.pname = pname;
        }
        public String getInfo() {
            return "省份ID:"+this.pid+"
    "+
                    "省份名字"+this.pname;    
        }
        
        public void setCity(City city []) {
            this.city = city;    
        }
        public City [] getCity() {
            return this.city;    
        }
    
    }
    
    class City {
        private int cid;
        private String cname;
        private Province province;
        public City(int cid, String cname) {
            this.cid = cid;
            this.cname = cname;
        }
        public String getInfo() {
            return "城市ID:"+ this.cid+"城市名字:"+this.cname;
        }
        public void setProvince(Province province) {
            this.province = province;
        }
        public Province getProvince() {
            return this.province;
        }
    }
    
    public class test1 {
        public static void main(String args[]) {
            //设置独立对象
            Province p = new Province(1,"重庆");
            City c_1 = new City(001,"渝中区");
            City c_2 = new City(002,"南岸区");
            //设置相关关系
            c_1.setProvince(p);//一个城市属于一个省份
            c_2.setProvince(p);
            p.setCity(new City [] {c_1,c_2});
            //获取数据
            for (int x=0 ;x<p.getCity().length;x++) {
                System.out.println(p.getCity()[x].getInfo());
            }
            System.out.println(c_1.getProvince().getInfo());
            System.out.println(c_2.getProvince().getInfo());
        
        }    
    }
    //输出
    城市ID:1城市名字:渝中区
    城市ID:2城市名字:南岸区
    省份ID:1
    省份名字重庆
    省份ID:1
    省份名字重庆

    实例2:

    //setter,getter,无参构造省略
    class Item {
        private int iid;
        private String iname;
        private String note;
        private Subitem subitem [];
        
        public Item(int iid, String iname,String note) {
            this.iid = iid;
            this.iname = iname;
            this.note = note;
        }
        public String getInfo() {
            return "类型ID:"+this.iid+"
    "+
                    "类型名字"+this.iname+"
    "+
                    "描述"+this.note;    
        }
        
        public void setSubitem (Subitem subitem []) {
            this.subitem = subitem;
        }
        public Subitem [] getSubitem() {
            return this.subitem;
        }
    }
    
    class Subitem {
        private int subid;
        private String subname;
        private String note;
        private Item item;
        private Product product [];
        
        public Subitem(int subid, String subname,String note) {
            this.subid = subid;
            this.subname = subname;
            this.note = note;
        }
        public String getInfo() {
            return "子类型ID:" +this.subid+"
    "+
                    "子类型名字:"+this.subname+"
    "+
                    "描述"+this.note;
        }
        public void setItem(Item item) {
            this.item = item;
        }
        public Item getItem() {
            return this.item;
        }
        public void setProduct(Product product []) {
            this.product = product;
        }
        public Product [] getProduct() {
            return this.product;
        }
    }
    
    class Product {
        private int pid;
        private String pname;
        private int price;
        private Subitem subitem;
        
        public Product(int pid, String pname, int price) {
            this.pid = pid;
            this.pname = pname;
            this.price = price;
        }
        public String getInfo() {
            return "商品ID"+this.pid+"
    "+
                    "商品名称"+this.pname+"
    "+
                    "价格"+this.price;
        }
        public void setSubitem(Subitem subitem) {
            this.subitem = subitem;
        }
        public Subitem  getSubitem() {
            return this.subitem;
        }
    }
    public class test1 {
        public static void main(String args[]) {
            //设置独立对象
            Item i = new Item(1,"食品类","吃的");
            
            Subitem sub1 = new Subitem(001,"肉类","猪肉");
            Subitem sub2= new Subitem(002,"蔬菜类","白菜");
            Subitem sub3 = new Subitem(003,"坚果类","瓜子");
            
            Product p1 = new Product(0001,"肥肉",20);
            Product p2= new Product(0002,"大白菜",5);
            Product p3 = new Product(0003,"甜瓜子",12);
            
            //设置相关关系
            sub1.setItem(i);//一个城市属于一个省份
            sub2.setItem(i);
            sub3.setItem(i);
            i.setSubitem(new Subitem [] {sub1,sub2,sub3});
            sub1.setProduct(new Product [] {p1});
            sub2.setProduct(new Product [] {p2});
            sub3.setProduct(new Product [] {p3});
            
            p1.setSubitem(sub1);
            p2.setSubitem(sub2);
            p3.setSubitem(sub3);
            //获取数据
            for (int x=0 ;x<i.getSubitem().length;x++) {
                System.out.println(i.getSubitem()[x].getInfo());
            }
            System.out.println(p3.getSubitem().getItem().getInfo());
        }    
    }
  • 相关阅读:
    RTT设备与驱动之串口
    RTT设备与驱动之PIN设备
    RTT之ENV
    MQTT学习之一
    思维导图软件
    英语单词学习方法
    RTT之POSIX
    10 个强大的JavaScript / jQuery 模板引擎推荐
    30个实用的jQuery选项卡/导航教程推荐
    jquery 自动完成 Autocomplete插件汇总
  • 原文地址:https://www.cnblogs.com/xhnxhnu/p/9119617.html
Copyright © 2011-2022 走看看