0 0

private or protected5

以前我写class 除了公有的方法就是私有的方法。只要不是给别人用的都写成私有的

现在为了测试方便习惯把testCase跟测试对象放在一个包里,然后把方法都写成protected方便测试。

这种方法好吗,什么时候应该用私有方法,什么时候用protected方法呢?
OO 
2008年7月23日 07:52

4个答案 按时间排序 按投票排序

0 0

采纳的答案

你的需求不应该用protected。

protected只用于子类继承需要override的情况。

你的需求用默认的包访问权限即可。请记住java有4种可见性控制,private、public、protected以及什么都不加时的包访问权限。

2008年7月24日 11:24
0 0

建议测试代码放到同一个包中,但最好放在不同的文件夹中。
例如在eclipse中,代码放到src中, 测试代码放到test中,包名可以相同.
如:com.abc.blabla

楼上说的对,protected是给之类继承时用的,如果不想给之类使用就用private。public的就不用说了。

2008年7月24日 11:04
0 0

如果一个类是需要被继承的,而且他的方法,子类又用得到,就用protected
话说回来,要理解这个问题,需要了解面向对象中,对象之间的关系
一般来说,如果对象之间是依赖关系,用public
而聚合关系一般都用protected的
如果这个方法不想和外界打交道就是private
总的来说,private是自己用的,protected是给子类继承用的

2008年7月23日 10:10
0 0

完全对外的: public
完全对内的: private
给子孙用的: protected

2008年7月23日 09:34

相关推荐

    Advanced EFS Data Recovery

    Files are being decrypted even in a case when the system is not bootable and so you cannot log on, and/or some encryption keys (private or master) have been tampered. Besides, decryption is possible ...

    2012RobocupRuscue东大代码

    * Whether to use AKSpeak messages or not. */ protected boolean useSpeak; /** * Construct a SEU Agent. */ protected SEUAgent() { importantmessage =new ImportantMessage(); smartchoose = new ...

    C 程序设计教学课件:Chapter 3 class and object.ppt

    These objects are instances of classes, which act as blueprints or templates. A class defines the properties and actions that its objects will have. For instance, in designing a car, the class might ...

    PowerMock资料大全(E文)

    For example final classes and methods cannot be used, private methods sometimes need to be protected or unnecessarily moved to a collaborator, static methods should be avoided completely and so on ...

    NIST SP800-57pt2r1.pdf

    In the case of symmetric key algorithms, the single key must be kept secret from everyone and everything not specifically authorized to access the information being protected. In asymmetric key ...

    VC帮助文档

    Occasionally, private members are listed because they override a public or protected member in the base class. See the class header files for a complete listing of class members. Some C-language ...

    带注释的Bootstrap.java

    * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding ...

    flex常见问题100题

    private,protected,internal,public 访问权限 private:只能在类本身内部访问,按惯例,命名私有成员时以下划线“_”开首;protected:可以由类本身或不论什么子类访问。但这是以实例为基础的。换言之,类实例可以...

    设计模式期中考试试卷

    protected Implementor implementor; public void SetImplementor(Implementor imp) { implementor = imp; } public abstract void Operation(); } public interface Implementor { void ...

    cloudtest-1.0.4-b201302231138PM Release

    It can test all methods including public protected, as well as private Automation regression is also build in framework None java coding jobs for test case development You can test you cases in every ...

    Android重写Gallery

    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); // Calculate our view width mGalleryWidth = right - ...

    android中简单碰撞

    change size or color ball.setRadius(ball.getRadius() * 1.5f); // Increase size ball.setColor(Color.RED); // Change color } // Draw the ball canvas.drawCircle(ball.getX(), ball.getY(), ball....

    c++如何控制对象的创建方式(禁止创建栈对象or堆对象)和创建的数量

    最佳的方法是将析构函数声明为private,而构造函数保持为public。这样可以限制栈对象的生成,同时也允许使用new来产生堆对象。 例如: ```cpp class NoStackObject{ protected: ~NoStackObject(){}; public: void...

    Data Guard Pro 1.0_HomeHome_DataGuardPro1.0_

    Data Guard Pro is a powerful and new computer security program that lets you create protected storage regions called Privacy Blocks in your local hard disk partitions where you can store your private ...

    cxf的jar包

    private static final String IMPLEMENTOR = "implementor"; public EndpointDefinitionParser() { setBeanClass(SpringEndpointImpl.class); } protected String getSuffix() { return ".jaxws-endpoint...

    ASP.NET 中打开光驱和关闭光驱

    Dim handle As IntPtr = CreateFile(devicePath, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero) If handle <> INVALID_HANDLE_VALUE Then ...

    kotlin for java or android 中文开发文档

    - **可见性修饰符**:Kotlin 支持 public、private、protected 和 internal 四种可见性级别。 - **扩展**:Kotlin 支持为现有的类添加新的功能,无需继承或封装。 - **数据类**:Kotlin 的数据类提供了一种方便的...

    自己写的DES加密解密 ASP.NET(C#)

    { private static byte[] Key64 = { 42, 16, 93, 156, 78, 4, 218, 32 }; private static byte[] IV64 = { 55, 103, 246, 79, 36, 99, 167, 3 }; protected void Page_Load(object sender, EventArgs e) { } ...

    uhdd.sys源码

    in CONFIG SYS or it can load later in AUTOEXEC BAT or by user command DOS can copy critical programs and data to the RAMdisk where they will be read or written at memory speed If loaded after ...

    ClassicJS:添加对ES6类的完全私有和受保护的支持,而不会阻止代理

    经典JS 这是一个旨在为ES6类提供完全特权成员支持的库。 这意味着您将充分使用当使用C#,Java和C ++等语言时可能已经习惯的访问说明。 您将获得所有这些,而不会...const { STATIC , PRIVATE , PROTECTED , PUBLIC ,

Global site tag (gtag.js) - Google Analytics