学习笔记——java类

一、学习重点

 二、学习内容整合案例

package com.jsoft.morning.test;

/*
    一汽车间:
    生产车:
    丰田、大众、奥迪......
    生产的车是不同型号的,但是车的编号是累加的...
    编号不属于任何一台车,属于车间的(不属于对象,属于类)
 */
public class WorkShop {
    public static String NO = "1001";

    public static Car product(String brand,String color){
        System.out.println("正在生产第"+NO+"号汽车!");
        int i = Integer.parseInt(NO);
        i++;
        NO = String.valueOf(i);
        return new Car(brand,color);
    }


}


package com.jsoft.morning.test;

public class Car {
    private String brand;
    private String color;

    public Car(String brand, String color) {
        this.brand = brand;
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Car() {
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}
package com.jsoft.morning.test;

public class Test {
    public static void main(String[] args) {
        WorkShop workShop = new WorkShop();
        System.out.println(workShop.product("大众", "白色"));
        System.out.println(workShop.product("丰田", "黑色"));
        System.out.println(workShop.product("奥迪", "蓝色"));
    }
}

双向链表

package com.jsoft.afternoon.doublelinked;

class Node {
    // 数据本身
    Integer data;
    // 上一个结点的地址
    Node prev;
    // 下一个结点的地址
    Node next;

    public Node() {
    }

    public Node(Integer data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                ", prev=" + prev +
                ", next=" + next +
                '}';
    }
}
public class DoubleLinked {



    // 维护一个头结点,头结点不要动,不存放具体的数据
    private Node head = new Node(0);

    // 返回头结点
    public Node getHead() {
        return head;
    }

    // 显示链表(遍历)
    public void list() {
        if(head.next == null){
            System.out.println("链表为空...");
            return;
        }
        // 因为头结点,不能动,我们需要一个辅助变量来帮我们遍历
        Node temp = head.next;
        while(true) {
            // 判断是否走到最后
            if(temp == null){
                break;
            }
            System.out.println(temp.data);
            // 将指针后移
            temp = temp.next;
        }
    }

    // 添加结点到链表最后
    // 默认添加的是到链表的最后一个位置
    // 找到链表的最后一个结点
    // 把这个结点的下一个指向当前结点
    // 当前结点的上一个结点指向之前的最后一个结点
    public void add(Node node) {
        // 因为我们头结点不能动,我们
        Node temp = head;
        while(true) {
            // 找到链表的表尾结点
            if(temp.next == null){
                break;
            }
            // 如果没有找到最后,将temp后移
            temp = temp.next;
        }
        // 当退出while循环时,temp指针就指向了最后
        temp.next = node;
        node.prev = temp;
    }

    public static void main(String[] args) {
        DoubleLinked doubleLinked = new DoubleLinked();
        doubleLinked.add(new Node(1));
        doubleLinked.add(new Node(2));
        doubleLinked.add(new Node(3));
        doubleLinked.add(new Node(4));

        doubleLinked.list();
//        System.out.println(doubleLinked.getHead());
    }
}

 

、笔记内容

 

posted @ 2022-07-27 23:01  LJMMJL  阅读(33)  评论(0)    收藏  举报