constructor

Why not Constructor in Anonymous class in java?Its contradicting OOPs rule [closed]

喜你入骨 提交于 2020-01-01 17:07:53
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . The oops rule is "No class can exist without constructor".its ok.But in java Anonymous class can'never have its constructor.because it does not have any name. So it is contradict OOPS rule..I m really confused.Is it breaking OOPS rule?Please help 回答1: Actually, they have one implicit constructor. Suppose you

How to handle bidirectional relationships when constructing hibernate entities?

半城伤御伤魂 提交于 2020-01-01 12:16:08
问题 I want to model the relationship between two entities, a group and an account with JPA/Hibernate. An account can have several groups, but not vice versa, so we have a OneToMany relationship between account and group. My working colleague suggested to model the entities Account and Group like public class Account { private List<Group> groups = new ArrayList<Group>(); public Account() {} public void setGroups(List<Group> usergroups) { this.groups = groups; } @OneToMany(mappedBy = "account")

How to handle bidirectional relationships when constructing hibernate entities?

一个人想着一个人 提交于 2020-01-01 12:16:04
问题 I want to model the relationship between two entities, a group and an account with JPA/Hibernate. An account can have several groups, but not vice versa, so we have a OneToMany relationship between account and group. My working colleague suggested to model the entities Account and Group like public class Account { private List<Group> groups = new ArrayList<Group>(); public Account() {} public void setGroups(List<Group> usergroups) { this.groups = groups; } @OneToMany(mappedBy = "account")

How can I check the failure in constructor() without using exceptions?

Deadly 提交于 2020-01-01 12:05:07
问题 All of the classes that I'm working on have Create()/Destroy() ( or Initialize()/Finalized() ) methods. The return value of the Create() method is bool like below. bool MyClass::Create(...); So I can check whether initialization of the instance is successful or not from the return value. Without Create()/Destroy() I can do the same job in constructor() and destructor() but I can't solve below problem. Can anyone help me? Thanks in advance. I cannot use exceptions because my company doesn't

Passing parameters in the Form constructor, winforms c#

坚强是说给别人听的谎言 提交于 2020-01-01 11:50:32
问题 I have a following inheritance hierarchy: Class A : Form Class B : Class A Class A needs to be able to accept a parameter so that I can create the instance of Class B like this: ClassB mynewFrm = new ClassB(param); How do I define such a constructor in Class A? thanks! I am using Winforms in .net 3.5, c# EDITED: Class A and Class B are defined as forms, using partial classes. So I guess this is turning into a question about partial classes and custom (overriden) constructors. 回答1: Here is a

Is a C# Constructor thread safe?

淺唱寂寞╮ 提交于 2020-01-01 09:36:49
问题 Let's say I have multiple threads and each of them is trying to create objects of the same class. Will the simultaneous creation of objects of the same type in different threads interfere with each other? Do I need to use "lock" within the constructor? 回答1: This depends very much on the implementation of the constructor. If the constructor is only accessing members of that class, and not any external static classes or methods, then yes - it is thread safe. But if that constructor is accessing

Is a C# Constructor thread safe?

帅比萌擦擦* 提交于 2020-01-01 09:36:07
问题 Let's say I have multiple threads and each of them is trying to create objects of the same class. Will the simultaneous creation of objects of the same type in different threads interfere with each other? Do I need to use "lock" within the constructor? 回答1: This depends very much on the implementation of the constructor. If the constructor is only accessing members of that class, and not any external static classes or methods, then yes - it is thread safe. But if that constructor is accessing

What's the reason of using implicit/explicit convertions instead of constructors?

╄→尐↘猪︶ㄣ 提交于 2020-01-01 09:33:38
问题 An example would be: XNamespace ns = "my namespace" Why not?: XNamespace ns = new XNamespace ( "my namespace" ) What's the idea behind using implicit/explicit convertions instead of constructors? Convenience? Is there a guideline for this? 回答1: Convenience? More or less, yes. Consider the case for when you’ve got a number-like object (say, a Complex ) on which you do calculations. Clearly, writing code such as: Complex result = c1 * new Complex(2) + new Complex(32); is very annoying and hard

Copy constructor: deep copying an abstract class

拜拜、爱过 提交于 2020-01-01 08:26:20
问题 Suppose I have the following (simplified case): class Color; class IColor { public: virtual Color getValue(const float u, const float v) const = 0; }; class Color : public IColor { public: float r,g,b; Color(float ar, float ag, float ab) : r(ar), g(ag), b(ab) {} Color getValue(const float u, const float v) const { return Color(r, g, b) } } class Material { private: IColor* _color; public: Material(); Material(const Material& m); } Now, is there any way for me to do a deep copy of the abstract

Foo f = Foo(); // no matching function for call to 'Foo::Foo(Foo)' … huh?

柔情痞子 提交于 2020-01-01 08:18:31
问题 class Foo { public: explicit Foo() {} explicit Foo(Foo&) {} }; Foo d = Foo(); error: no matching function for call to 'Foo::Foo(Foo)' I tried changing Foo(Foo&) to Foo(Foo) as the error suggests, which AFAIK is not a valid constructor, and sure enough I get: error: invalid constructor; you probably meant ‘Foo (const Foo&)’ What gives? How do I resolve this? (This is on GCC by the way) 回答1: There are two questionable things that you have in your copy constructor. First, you've made the copy