encapsulation

PHP5 member visibility

纵饮孤独 提交于 2019-12-12 01:42:39
问题 Could someone explain me, why is it possible to do the following in PHP, but, for example, not in C# or Java: Class A { protected $a = 'Howdy!'; } Class B extends A { public function howdy() { $created = new A(); echo $created->a; <----- This is legal due to per-class visibility } } $b = new B(); echo $b->howdy(); <----- Hence, no fatal error here This behavior seems to be specified here, but I can't understand the fundamental reason behind this (to my mind, one can't simply implement the per

an efficient matrix interface in C++

一世执手 提交于 2019-12-12 01:28:16
问题 I am writing a code which uses matrices and advanced operations on them a lot. I am not going to implement a matrix class from the beginning instead I want to have some sort of Matrix abstract class or interface which I define the methods I want for it but it uses another implemented Matrix class through it's core something like: template <typename T> class IMatrix { public: /*some methods and also some operator overloads*/ virtual T operator() (int r, int c)=0; }; as the interface for my

protection level for model attributes in rails

橙三吉。 提交于 2019-12-11 13:05:54
问题 Suppose the following situation class User < ActiveRecord::Base private def password= p self[:password] = p end def password self[:password] end end If anyone with access to the Rails console can do: Loading development environment (Rails 4.0.0) 2.0.0p247 :001 > User => User(id: integer, name:string, password:string) 2.0.0p247 :002 > u = User.find(1) => #<User id: 1, name: "Jack", password: "da6c253ffe0975ca1ddd92865ff3d5f0"> 2.0.0p247 :003 > u.password = "123" NoMethodError: private method

Java good practice - Declarations of interface types instead of declarations of implementation types

房东的猫 提交于 2019-12-11 10:59:41
问题 In "O'Reilly - Programming Android" they recommend avoiding this code: ArrayList<String> a = new ArrayList<String>(); but to replace it with this: List<String> a = new ArrayList<String>(); They argue it's easier to maintain code if later the type of a should be changed to say a linked list. If so, why not make it of type Collection , or even Object ? I feel that as the instantiation must be changed to change its type, surely it's better to keep it type restricted as much as possible and

Why parent class type reference variable having reference to child class object can't access child class's Methods

爷,独闯天下 提交于 2019-12-11 08:59:29
问题 Since this object(stated in title) can invoke overridden methods in child class, why it can't invoke other methods of child class? I need answer as detailed as possible like memory organization, internal logic in JVM etc. below code will give you clear understanding of my question. class A { int x=10; public A() { System.out.println("Constructor of class A called!!!"); } public void sayGreetings() { System.out.println("accept hye from class A"); } } class C extends A { int x=30;//why this is

If a variable is enclosed, and an instance sets a property with the same name, where does the property go?

若如初见. 提交于 2019-12-11 08:36:24
问题 Most of us know that JavaScript has no sense of "private" properties, but that behavior can be emulated through the use of closures: var Car = function () { var name = 'Tesla'; var wheelCount = 4; this.getName = function () { return name; }; this.getWheelCount = function () { return wheelCount; }; this.setName = function (newName) { name = newName; }; this.setWheelCount = function (newCount) { wheelCount = newCount; }; }; var myCar = new Car(); console.log(myCar.name); // undefined That all

Encapsulation in JavaScript with getter and setter

送分小仙女□ 提交于 2019-12-11 06:58:42
问题 I realise that this has been asked but have researched and failed - sorry! I want to implement encapsulation in JS as simply as possible. I realise that any 'var' in the class will be private. I am simply unsure how to GET and SET values of any private var. In the example below the interface methods for GETTING and SETTING 'colour' do not work because those functions cannot access the private 'colour' property of the object. I cannot find a clear example showing me how to implement this. I am

Fortran: Calling a function in a module from a procedure in another module

北慕城南 提交于 2019-12-11 06:13:07
问题 I admit the title might be a bit obscure, so let me give an example of what I want to do and what doesn't work. I have a main program which calls a subroutine which is in a module: Program Test_program Use module_A Implicit none Integer :: i i = 1 call subroutine_A(i) End program Test_program this subroutine_A is in module A, and in turns calls a function_B which is in module_B: module module_A use module_B implicit none contains subroutine subroutine_A(i) implicit none integer, intent(in) ::

Why hide a class implementation?

左心房为你撑大大i 提交于 2019-12-11 04:43:40
问题 I'm stuck on this concept. This is part of an explanation I saw on a site: Hiding the Implementation A primary consideration in object-oriented design is separating the things that change from the things that stay the same. This is particularly important for libraries. Users (client programmers) of that library must be able to rely on the part they use, and know that they won't need to rewrite code if a new version of the library comes out. On the flip side, the library creator must have the

Class design with vector as a private/public member?

折月煮酒 提交于 2019-12-11 03:33:30
问题 what is the best way to put a container class or a some other class inside a class as private or a public member? Requirements: 1.Vector< someclass> inside my class 2.Add and count of vector is needed interface 回答1: If the container's state is part of the class's invariant, then it should, if possible, be private. For example, if the container represents a three dimensional vector then part of the invariant might be that it always contains exactly 3 numbers. Exposing it as a public member