1. 程式人生 > 其它 >linux中網絡卡的流量怎麼通過c語言獲取_用Python獲取計算機網絡卡資訊

linux中網絡卡的流量怎麼通過c語言獲取_用Python獲取計算機網絡卡資訊

工廠模式提供了建立物件的最佳方式,建立物件時不會向客戶端暴露建立邏輯,並且通過一個共同的介面指向新建立的物件。

1、建立一個介面

public interface Shape {
    void draw();
}

2、具體的物件

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}
public class Rectangle implements
Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } }
public class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}

3、定義工廠類

public class ShapeFactory {
    
    
public Shape getShape(String shapeType) { if (null == shapeType) { return null; } switch (shapeType) { case "CIRCLE": return new Circle(); case "RECTANGLE": return new Rectangle(); case "SQUARE":
return new Square(); default: return null; } } }

4、測試

public class _Test {
    public static void main(String[] args) {
        ShapeFactory factory = new ShapeFactory();
        Shape shape1 = factory.getShape("CIRCLE");
        shape1.draw();
        Shape shape2 = factory.getShape("RECTANGLE");
        shape2.draw();
        Shape shape3 = factory.getShape("SQUARE");
        shape3.draw();
    }
}