有人可以解释一下为什么这段代码:
class safe_bool_base
{ //13
protected:
typedef void (safe_bool_base::*bool_type)() const;
void this_type_does_not_support_comparisons() const {} //18
safe_bool_base() {}
safe_bool_base(const safe_bool_base&) {}
safe_bool_base& operator=(const safe_bool_base&) { return *this; }
~safe_bool_base() {}
};
template <typename T=void> class safe_bool : public safe_bool_base
{
public:
operator bool_type() const
{
return (static_cast<const T*>(this))->boolean_test() ? &safe_bool_base::this_type_does_not_support_comparisons : 0;
}
protected:
~safe_bool() {}
};
template <> class safe_bool<void> : public safe_bool_base
{
public:
operator bool_type() const
{
return (boolean_test() == true) ? &safe_bool_base::this_type_does_not_support_comparisons : 0; //46
}
protected: …Run Code Online (Sandbox Code Playgroud) 我想知道在Fortran中是否有一种全局变量的方法,可以说是某种"受保护".我在想一个包含变量列表的模块A. 使用A的每个其他模块或子例程都可以使用它的变量.如果你知道变量的值是什么,你可以使用参数来实现它不能被覆盖.但是如果你必须先运行代码来确定变量值呢?您无法将其声明为参数,因为您需要更改它.有没有办法在运行时的某个特定点做类似的事情?
我想在Python 3.2中使用'protected'访问来设置一个类层次结构:基类的成员只在派生类的范围内,而不是'public'.
双下划线使成员"私有",单个下划线表示警告但成员仍为"公共".什么(如果有的话)是指定"受保护"成员的正确语法.
我的问题与Java有关,但它也可以应用于C#.我想知道为什么每个人都建议将实例变量设为私有而不是让它们受到保护.
让我们考虑一下.子类看不到私有变量,所以如果我需要访问或更改子类中超类的变量,我被迫使用一些访问器和mutators方法,如getMyPrivateVariable或setMyPrivateVariable.但是,当您扩展某个类并继承其成员时,这就像您直接在子类中声明它们一样.从逻辑上讲,这意味着子类也应该直接访问实例变量,并为使用受保护变量设计类提供了理由.我理解这种做法会破坏封装,但这在继承的情况下似乎无关紧要,因为在这种情况下,一切都像超级类的成员在sublcass中声明一样,所以sublcass有一个"自然权利"能够直接访问其成员,无论他们是否继承.在我看来,封装对于通过对象的继承树之外的其他对象与对象进行交互更为重要.
所以,我的问题是为什么每个人都建议将类的实例变量声明为私有而不是受保护?
尝试在公共类中创建受保护的内部类的受保护内部成员会导致以下问题:
可访问性不一致:字段类型'what.Class1.ProtectedInternalClass'比字段'what.Class1.SomeDataProvider.data'更难访问
据我所知,可访问性应该是等效的.
我哪里弄错了?
起源类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace what
{
public class Class1
{
// This class cannot be modified, is only
// here to produce a complete example.
public class PublicClass
{
public PublicClass() { }
}
protected internal class ProtectedInternalClass : PublicClass
{
public ProtectedInternalClass() { }
public void SomeExtraFunction() { }
}
public class SomeDataProvider
{
public int AnInterestingValue;
public int AnotherInterestingValue;
protected internal ProtectedInternalClass data; //<--- Occurs here.
public PublicClass …Run Code Online (Sandbox Code Playgroud) 是否A::foo需要公开B宣布为朋友?
class A {
protected: // public ?
void foo(int x);
};
class B : public A {
friend void A::foo(int); // not fine with GCC 4.8.1 but fine with VS 2013
void goo(int x) {foo(x);} // fine
static void hoo(int x) {}
};
void A::foo(int x) {B::hoo(x);} // friend declaration needed for this
Run Code Online (Sandbox Code Playgroud)
如果A :: foo受到保护,Visual Studio 2013认为没问题,但GCC 4.8.1认为它不是,并希望它是公开的.哪个编译器正确?我最初的直觉是它可以被宣布为受保护.毕竟,B是从A派生的,所以应该可以访问A :: foo(因为B :: goo简单地演示).
嗨,我有一个抽象类,其中有一些公共方法和一些抽象方法.我有公众,所以他们实现派生类的常用方法.
令我困惑的是为什么我想要定义一个公共抽象方法而不是保护抽象.对于我来说,在抽象类中定义一个公共抽象方法是没有意义的....因为如果是抽象将在派生类中被覆盖,但同样如果被定义为public,但不知何故定义更有意义它受到保护,因为我们知道我们将在派生类中覆盖它.
将方法定义为抽象类中的公共抽象是错误的吗?哪个更好?为什么?
我从Core Java,第I卷 - 基础知识(第8版)>第5章:继承>'受保护的访问'一节(页221)一书中学到了以下内容.
但是,有时候,您只想将方法限制为子类,或者不太常见,允许子类方法访问超类字段.在这种情况下,您将类功能声明为protected.例如,如果超类Employee将hireDay字段声明为protected而不是private,则Manager方法可以直接访问它.
但是,Manager类方法只能查看Manager对象的hireDay字段,而不能查看其他Employee对象.这种限制是为了使您不能滥用受保护机制并形成子类只是为了获得对受保护字段的访问权限.
我编写了以下代码来测试它.
class Employee
{
protected String name;
public Employee(String name) {
this.name = name;
}
}
class Manager extends Employee
{
public Manager(String name) {
super(name);
}
public void peekName(Employee e) {
System.out.println("name: " + e.name);
}
}
class Executive extends Employee
{
public Executive(String name) {
super(name);
}
}
public class TestProtectedAccess
{
public static void main(String[] args) {
Employee e = new Employee("Alice Employee");
Manager m = new Manager("Bob Manager"); …Run Code Online (Sandbox Code Playgroud) 我从子类调用超类'受保护的方法.为什么这种方法"不可见"?
我一直在读一些职位如这一个,这似乎违背了以下内容:
超级课程:
package com.first;
public class Base
{
protected void sayHello()
{
System.out.println("hi!");
}
}
Run Code Online (Sandbox Code Playgroud)
子类:
package com.second;
import com.first.Base;
public class BaseChild extends Base
{
Base base = new Base();
@Override
protected void sayHello()
{
super.sayHello(); //OK :)
base.sayHello(); //Hmmm... "The method sayHello() from the type Base is not visible" ?!?
}
}
Run Code Online (Sandbox Code Playgroud) 我知道如何启动华为中列出受保护应用程序的活动,使用:
但我需要知道是否存在一些方法来获取字符串中受保护的应用程序列表并验证我的应用程序是否受到保护.
因为我的应用程序有一个唤醒锁,一个服务甚至一个JobService,华为仍在杀死它.
但我认为将其从受保护列表中删除是避免这种情况的好方法.
有一些方法可以获取受保护的应用程序列表,或者避免我的应用程序被华为EMUI系统杀死.