会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133662个问题
Python 全系列/第一阶段:Python入门/序列 26521楼

''''
使用工厂模式、单例模式实现如下需求:
(1) 电脑工厂类 ComputerFactory 用于生产电脑 Computer。工厂类使用单例模式,也就是说只能有一个工厂对象。
(2) 工厂类中可以生产各种品牌的电脑:联想、华硕、神舟
(3) 各种品牌的电脑使用继承实现:
(4) 父类是 Computer 类,定义了 calculate 方法
(5) 各品牌电脑类需要重写父类的 calculate
'''
class Computer:
    __obj=None
    __init__flag=True
    def create_computer(self,brand):
        if brand=='联想':
            return Lenovo(Computer)
        if brand=='华硕':
            return Asus(Computer)
        if brand=='神舟':
            return Hasee(Computer)
        else:
            return '未知品牌,无法创建'
    def calculate(self):
        print('生产电脑')
    def __new__(cls, *args, **kwargs):
        if cls.__obj==None:
            cls.__obj=object.__init__(cls)
            return cls.__obj
    def __init__(self,brand):
        if self.__init__flag:
            print('init...')
            self.__init__flag=False

class Lenovo(Computer):
    def calculate(self):
        print('生产联想电脑')
class Asus(Computer):
    def calculate(self):
        print('生产华硕电脑')
class Hasee(Computer):
    def calculate(self):
        print('生产神舟电脑')
factory=Computer()
factory2=Computer()
x=factory.create_computer('联想')

image.png

老师,我这个代码一直报错,看了问答区感觉我代码好像没错啊

Python 全系列/第一阶段:Python入门/面向对象 26523楼
JAVA 全系列/第六阶段:JavaWeb开发/Web实战案例 26524楼
微服务/第十八阶段:数字货币交易所项目(Spring Cloud Alibaba架构)/服务中台_后台管理系统的开发 26525楼

为什么我这个生产者只生产娃哈哈 , 而且 后面的null值是怎么回事儿?

public class Goods {
    private String name;//商品名字
    private String brand;//

    public Goods() {
    }

    public Goods(String name, String brand) {
        this.name = name;
        this.brand = brand;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    public String getName() {
        return name;
    }

    public String getBrand() {
        return brand;
    }
    //编写一个赋值方法,同步监视器就为当前对象
    public synchronized void set(String nane ,String brand) throws InterruptedException {
        this.setName(name);
        Thread.sleep(300);
        this.setBrand(brand);
        System.out.println("生产者线程生产了:"+this.getBrand()+"---"+this.getName());
    }
    public synchronized void get ()throws InterruptedException{
        System.out.println("消费者线程取走了:"+this.getBrand()+"---"+this.getName());
    }
}

结果:

/**

生产者线程生产了:娃哈哈---null
消费者线程取走了:娃哈哈---null
消费者线程取走了:娃哈哈---null
消费者线程取走了:娃哈哈---null
消费者线程取走了:娃哈哈---null
消费者线程取走了:娃哈哈---null
消费者线程取走了:娃哈哈---null
消费者线程取走了:娃哈哈---null
消费者线程取走了:娃哈哈---null
消费者线程取走了:娃哈哈---null
消费者线程取走了:娃哈哈---null
生产者线程生产了:娃哈哈---null
生产者线程生产了:娃哈哈---null
生产者线程生产了:娃哈哈---null
生产者线程生产了:娃哈哈---null
生产者线程生产了:娃哈哈---null
生产者线程生产了:娃哈哈---null
生产者线程生产了:娃哈哈---null
生产者线程生产了:娃哈哈---null
生产者线程生产了:娃哈哈---null

Process finished with exit code 0

*/


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 26526楼
Python 全系列/第一阶段:Python入门/面向对象 26527楼
JAVA 全系列/(隐藏)第三十阶段:设计模式/框架源码分析(拓展)/GOF23 设计模式 26530楼
JAVA 全系列/第八阶段:Linux入门到实战/Maven 26533楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自定义右键菜单</title>
    <style>
        *{margin: 0;padding: 0}
      ul{
          list-style: none;
          width: 190px;
          height: 125px;
          background-color: darkgrey;
          position: absolute;
          display: none;
      }
        ul li{
            border: 1px solid skyblue;
            padding: 4px;
            cursor: pointer;
            transition: 0.5s;
        }
        ul li:hover{
            background-color: skyblue;
            color: #fff;
        }
    </style>
</head>
<body>

<ul>
    <li>我想尚学堂了!</li>
    <li>您真要离开此页面吗?</li>
    <li>去百度搜索页面中的内容</li>
    <li>输入内容后去百度搜索</li>
</ul>
<textarea cols="80" rows="20"></textarea>
<input type="text">
<script>
    let ul=document.querySelector('ul');
    //禁用系统的右键
    document.oncontextmenu=function (eve) {
        return false; //return false 表示禁用事件
    };
    //鼠标抬起出现鼠标右键菜单
    document.onmouseup=function (eve) {
        //eve.button 判断鼠标点的是那个按键
        if (eve.button==2){
            ul.style.display='inline-block';
            //设置鼠标的位置
            ul.style.left=eve.clientX+'px';
            ul.style.top=eve.clientY+'px';
        }else {
            ul.style.display='none';
        }

    }
    //点击某一个菜单选项时触发的事件(事件委托)
    ul.onmousedown=function (eve) {
        if (eve.target.innerHTML=='我想尚学堂了!'){
            alert('那就去吧!');
        }else if (eve.target.innerHTML=='您真要离开此页面吗?'){
            if (confirm('您真要离开此页面吗?')){
                window.close();
            }
        }else if (eve.target.innerHTML=='去百度搜索页面中的内容'){
            let result = document.getSelection().toString();
            open('https://www.baidu.com/s?wd='+result);
        }else {eve.target.innerHTML=='输入内容后去百度搜索'}{
            let value = document.querySelector('input').value;
            open('https://www.baidu.com/s?wd='+value);
        }
    }


</script>
</body>
</html>

image.png

怎么会取不到input框的值,去百度搜索呢,老师

WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 26534楼

'''
设计一个名为 MyRectangle 的矩形类来表示矩形。这个类包含
(1) 左上角顶点的坐标:x,y
(2) 宽度和高度:width、height
(3) 构造方法:传入 x,y,width,height。如果(x,y)不传则默认是 0,如果 width和 height 不传,则默认是 100.
(4) 定义一个 getArea() 计算面积的方法
(5) 定义一个 getPerimeter(),计算周长的方法
(6) 定义一个 draw()方法,使用海龟绘图绘制出这个
'''
import turtle as t
class MyRectangle:
    def __init__(self,x=0,y=0,width=100,height=100):
        self.x=x
        self.y=y
        self.width=width
        self.height=height
    @classmethod
    def message(self):
        m,n=eval(input('请输入左上角顶点的坐标(输入q表示选用默认值):'))
        w,h=eval(input('请输入矩形的宽度、高度(输入q表示选用默认值):'))
        if m =='q':
            print('坐标x用默认值')
        else:
            self.x=m
        if n =='q':
            print('坐标y用默认值')
        else:
            self.y=n
        if w =='q':
            print('宽度用默认值')
        else:
            self.width=m
        if h =='q':
            print('高度用默认值')
        else:
            self.height=h
    def getArea(self):
        s=self.width*self.height
        print('矩形的面积为:'.format(s))
    def getPerimeter(self):
        c=(self.width+self.height)*2
        print('矩形的周长为:'.format(c))
    def draw(self):
        t.Pen()
        t.goto(self.x,self.y)
        t.goto(self.x+self.width,self.y)
        t.goto(self.x+self.width,self.y-self.height)
        t.goto(self.x,self.y-self.height)
        t.goto(self.x,self.y)
        t.done()
MyRectangle.message()

老师,我这个作业写到这里就不会了,我不知道怎么把输入的数字传到类里面???

Python 全系列/第一阶段:Python入门/面向对象 26535楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637