encapsulation

Calling a second level base class constructor

╄→гoц情女王★ 提交于 2019-12-11 03:27:06
问题 In C#, I have a class, foo , that inherits from class bar , which in turn inherits from class foobar . In the foo constructor, I want to call the foobar constructor directly, not from a call to the bar constructor. I tried something like this: public foo(int i): base : base(i) Obviously this does not work. How would you make it work, without passing through the bar constructor? 回答1: You can't, as you cannot skip the initialization of bar. You have to create a constructor in bar that passes

Initialize member of abstract class without subclasses having write access

烂漫一生 提交于 2019-12-11 02:38:51
问题 I have an abstract class: public abstract class AbstractCommand { private static State state; } Intention An object of class State is provided by some "controlling classes", providing data that is needed by each AbstractCommand subclass Each subclass needs read access to it The subclasses are not allowd to change the field Current approach The field state should be initialized by the "controlling classes" of the program so that subclasses (that define commands) can use it (read-only). The

eC (Ecere) how to not worry about private data fields of a class

那年仲夏 提交于 2019-12-10 23:38:33
问题 In exposing the C++ (or Java) interface of a library, one has to provide the "private" fields of classes, and this is understable, because the compiler needs to know the structure of the class, in order to be able to compute, for example, sizeof(). But why this is needed and how could be alleviated? Because, for me, it appears as a breach in the encapsulation concept: why the user shall worry or have access to something that is considered to be private? One solution would be to define a size(

Asp.Net: Returning a Reader from a Class

独自空忆成欢 提交于 2019-12-10 19:16:38
问题 I was just wondering about the correct way to return a reader from a class? My code below works, but I'm unsure if this is correct. Also. I can't close the the connection in my class method and still access it from my ascx page, is that OK? // In my class I have the following method to return the record/reader -- it's a single record in this case. public SqlDataReader GetPost() { SqlConnection conn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("con_spPost", conn); cmd

How do I correctly wrap a Dictionary<T,U> and expose an enumerator?

大城市里の小女人 提交于 2019-12-10 17:00:05
问题 I am encapsulating a Dictionary in my object. How do I expose IEnumerable>? Before class HashRunningTotalDB : Dictionary<int, SummaryEntity> { /... } // WORKS! static void Main () { HashRunningTotalDB tempDB = new HashRunningTotalDB(); //todo: load temp DB foreach(var item in tempDB) { Console.Writeline(item.Key + " " + item.Value.SomeProperty); } } After class HashRunningTotalDB : IEnumerable { Dictionary<int, SummaryEntity> thisHashRunningTotalDB = new Dictionary<int, SummaryEntity>(); /

Appropriate method encapsulation for unit testing

心已入冬 提交于 2019-12-10 15:32:29
问题 My class contains 14 private methods and 1 public method . The public method calls all the private method directly or indirectly via other private methods. The public method also has a call to a DAO that queries the database. I wrote a unit test for the class. Since you can't write unit test for private methods, I changed all the private methods to default access and wrote unit test for them. I was told that I shouldn't change the encapsulation just for the purpose of testing. But my public

Return & print field value or just print value through class method?

北城以北 提交于 2019-12-10 14:28:51
问题 I'm currently learning Java and learning about encapsulation and I'm unsure which of the following is a better practice: Use a getter to return a field value from one class to another and then print it through a method in another class. Call a method in a class from another class to print the value of the field. The value isn't manipulated, only shown through System.out.println(); Any advice would be appreciated :) EDIT: One class (Person) holds information about people, such as name, age,

What does 'fidelity' of accessor keywords mean?

青春壹個敷衍的年華 提交于 2019-12-10 14:10:21
问题 I'm reading through .Net Docs and i came across this term "fidelity", Type safety is also used to help enforce encapsulation by guaranteeing the fidelity of the accessor keywords. What does it mean (in relation to accessor keywords)? 回答1: Sigh. There is simply too much documentation and not enough time for the development team to review it for accuracy in jargon. This overview is a mess of small errors and confusing, non-standard jargon usages. The paragraph in question is: Type safety is

Public variables in Python classes?

为君一笑 提交于 2019-12-10 13:01:51
问题 I am learning Python classes on my own right now and came across this page: http://www.tutorialspoint.com/python/python_classes_objects.htm The variable empCount is a class variable whose value would be shared among all instances of a this class. This can be accessed as Employee.empCount from inside the class or outside the class. I'm assuming this is called a public variable? Or a static public variable? Is this technically good practice? I know this question is a bit soft, but generally

How to encapsulate a custom MediaRecorder in Android Kotlin?

耗尽温柔 提交于 2019-12-10 12:09:16
问题 I am new to Kotlin programming. I use the following code to record audio as part of my AudioRecorderDialogFragment: fun startVoiceRecorder(voiceFilename: String) { if (mAudioRecorder == null) { // We don't have an AudioRecorder, so we build one mAudioRecorder = MediaRecorder() mAudioRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC) mAudioRecorder?.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4) mAudioRecorder?.setAudioEncoder(MediaRecorder.OutputFormat.MPEG_4) val audioOutputFile =