会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132376个问题
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 37261楼
JAVA 全系列/(旧的隐藏)第八阶段:电商高级项目_架构/编码/电商ego-基于SOA架构_Dubbo使用_逆向工程_分页插件完成商品查询 37263楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 37264楼

public class Test {
	public static void main(String[] args) {
//		File srcFile = new File("D:\\java10086\\test1.txt");
//		File targetFile = new File("E:\\test1.txt");
//		copyFile(srcFile, targetFile);
		File srcDir = new File("D\\java10086");
		File targetDir = new File("E\\java10086");
		copyDir(srcDir,targetDir);
	}
	public static void copyDir(File srcDir,File targetDir) {
		if (!targetDir.exists()) {
			targetDir.mkdir();//如果目的地的目录不存在,则需要使用File类方法进行创建目录
		}
		File[] files = srcDir.listFiles();//获取指定目录下的所有File对象
		for (File file : files) {
			if (file.isFile()) {
				copyFile(new File(srcDir+"\\"+file.getName()), new File(targetDir+"\\"+file.getName()));
			}
			else {
				copyFile(new File(srcDir+"\\"+file.getName()), new File(targetDir+"\\"+file.getName()));
			}
		}
	}
	public static void copyFile(File srcFile,File targetFile) {
		//(1)提高读写效率,从数据源
		BufferedInputStream bis =null;
		//(2)提高写入效率,写到目的地
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(srcFile));
			bos = new BufferedOutputStream(new FileOutputStream(targetFile));
			//(3)边读边写
			byte [] buf = new byte[1024];
			int len = 0;
			while ((len=bis.read(buf))!=-1) {
				bos.write(buf,0,len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//(4)关闭
			if (bos!=null) {
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (bis!=null) {
				try {
					bis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
		
	}
}

出现空指针异常的错误。不知道哪里错了?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 37266楼

class ComputerFactory:
    __obj = None
    __init_flag = True

    def __new__(cls, *args, **kwargs):
        if cls.__obj == None:
            cls.__obj == object.__new__(cls)
        return cls.__obj

    def creat_pc(self,brand):
        if brand == "联想":
            print("造了一个联想电脑")
            return Lianxiang()
        elif brand == "华硕":
            print("造了一个华硕电脑")
            return Huashuo()
        elif brand == "神州":
            print("造了一个神州电脑")
            return Shenzhou()
        else :
            return "请输入正确的电脑品牌"

    def __init__(self):
        if ComputerFactory.__init_flag:
            print("初始化代码执行了一次")
            ComputerFactory.__init_flag = False


class Computer:
    def calcute(self):
        print("我是电脑父类的calcute方法...")

class Huashuo(Computer):
    def calcute(self):
        print("我是华硕的calcute方法...")

class Lianxiang(Computer):
    def calcute(self):
        print("我是联想的calcute方法...")

class Shenzhou(Computer):
    def calcute(self):
        print("我是神州的calcute方法...")

fac = ComputerFactory()
lianxiang1 = fac.creat_pc("联想")
lianxiang1.calcute()

老师 能帮忙看下这个错是怎么回事吗:

    lianxiang1 = fac.creat_pc("联想")

AttributeError: 'NoneType' object has no attribute 'creat_pc'


Python 全系列/第一阶段:Python入门/面向对象 37267楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 37269楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/VSFTPD 37270楼
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 37271楼
Python 全系列/第一阶段:Python入门/面向对象 37275楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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