private

Public property VS Private property with getter?

荒凉一梦 提交于 2019-11-28 07:48:12
问题 This question has puzzled me for a while. A public property that can be accessed directly or a private property with getter? Which one is better/correct and why? 回答1: Exposing fields directly is considered a bad practice. It is better to keep the field private and only expose the getter and setter. One advantage is that you can choose different access levels for the getter and setter, whereas a field has only a single access level. Another advantage of using getters is that it allows you to

Public and private inheritance in C++

巧了我就是萌 提交于 2019-11-28 07:40:26
As we know from the literature for the public inheritance the object of child class (sub-class) also can be considered as the object of base class (super-class). Why the object of the sub-class can’t be considered as an object of super-class, when the inheritance is protected or private? Because you can't see it: class Base { public: virtual ~Base() {} }; class PublicDerived: public Base { }; class PrivateDerived: private Base { }; int main() { PublicDerived publicD; PrivateDerived privateD; Base& base1 = publicD; Base& base2 = privateD; // ERROR } So you can not use a PrivateDerived object

How to establish a SecIdentityRef in an iPhone keychain ? (Without a .p12)

北战南征 提交于 2019-11-28 07:40:14
How do you create a SecIdentityRef in an iPhone keychain if 1) you already have the private key in the keychain and 2) you have just received the certificate from a CA? SecPKCS12Import does not help in this case unless there is an API to create a .p12 from a private key and a certificate. SecIdentityCreateWithCertificate would be the answer on the Mac but it does not exist on the iPhone. Is it possible using SecItemAdd ? http://developer.apple.com/library/ios/#documentation/Security/Reference/keychainservices/Reference/reference.html many thanks, Andrew OK, to answer my own question: On iOS

Why would the conversion between derived* to base* fails with private inheritance?

蹲街弑〆低调 提交于 2019-11-28 07:29:46
Here is my code - #include<iostream> using namespace std; class base { public: void sid() { } }; class derived : private base { public: void sid() { } }; int main() { base * ptr; ptr = new derived; // error: 'base' is an inaccessible base of 'derived' ptr->sid(); return 0; } This gives a compile time error. error: 'base' is an inaccessible base of 'derived' Since the compiler will try and call the base class sid() why do I get this error? Can someone please explain this. Chubsdad $11.2/4 states- A base class B of N is accessible at R, if an invented public member of B would be a public member

Why is there a private access modifier in an abstract class in Java, even though we cannot create an instance of an abstract class?

旧巷老猫 提交于 2019-11-28 06:54:41
I know it is not a good coding practice to declare a method as private in an abstract class. Even though we cannot create an instance of an abstract class, why is the private access modifier available within an abstract class, and what is the scope of it within an abstract class? In which scenario is the private access specifier used in an abstract class? check out this code where Vehicle class is abstract and Car extends Vehicle. package com.vehicle; abstract class Vehicle { // What is the scope of the private access modifier within an abstract class, even though method below cannot be

Python inheritance - how to disable a function

雨燕双飞 提交于 2019-11-28 06:47:37
In C++ you can disable a function in parent's class by declaring it as private in the child class. How can this be done in Python? I.E. How can I hide parent's function from child's public interface? There really aren't any true "private" attributes or methods in Python. One thing you can do is simply override the method you don't want in the subclass, and raise an exception: >>> class Foo( object ): ... def foo( self ): ... print 'FOO!' ... >>> class Bar( Foo ): ... def foo( self ): ... raise AttributeError( "'Bar' object has no attribute 'foo'" ) ... >>> b = Bar() >>> b.foo() Traceback (most

Why should I use a private variable in a property accessor?

二次信任 提交于 2019-11-28 06:22:55
Sorry If I am being noob, I have this doubt, why do we use private variables and set them using properties ? Why can't we just use properites alone ? I am talking about situations like this private string _testVariable; public string MyProperty { get { return _testVariable;} set {_testVariable = value;} } I am thinking of simply using public string MyProperty { get; set; } Why redundant private variable? are these two strategies different ? can anyone please throw some light on this. Thanks Your examples are semantically the same. The concise property declaration syntax (just having { get; set

Is there a way to make Rails ActiveRecord attributes private?

老子叫甜甜 提交于 2019-11-28 06:16:33
By default, ActiveRecord takes all fields from the corresponding database table and creates public attributes for all of them. I think that it's reasonable not to make all attributes in a model public. Even more, exposing attributes that are meant for internal use clutters the model's interface and violates the encapsulation principle. So, is there a way to make some of the attributes literally private ? Or, maybe I should move on to some other ORM? Jordini was most of the way there Most of active_record happens in method_missing. If you define the method up front, it won't hit method_missing

groovy call private method in Java super class

北城以北 提交于 2019-11-28 04:49:25
问题 I have an abstract Java class MyAbstractClass with a private method. There is a concrete implementation MyConcreteClass . public class MyAbstractClass { private void somePrivateMethod(); } public class MyConcreteClass extends MyAbstractClass { // implementation details } In my groovy test class I have class MyAbstractClassTest { void myTestMethod() { MyAbstractClass mac = new MyConcreteClass() mac.somePrivateMethod() } } I get an error that there is no such method signature for

Do Sub-Classes Really Inherit Private Member Variables?

穿精又带淫゛_ 提交于 2019-11-28 04:46:59
Basically as far as I know, when you create a base class with a public, protected, and private section and variables/functions in each the public and protected sections will get inherited into the appropriate section of the sub-class (defined by class subclass : private base, which will take all public and protected members of base and put them into public, changing the word private to public puts them all in public and changing it to protected puts them all into protected). So, when you create a sub-class you never receive anything from the private section of the previous class (the base