In C#, why when we can define a class method and access to a method by class name directly, we should/some people define an object instead and then create an instance of that object?
Class1:
class Class1
{
public static int PrintX(int x)
{
return x;
}
private int y;
public int PrintY(int z)
{
return this.y = z;
}
}
Main Method:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Class1.PrintX(9));
Class1 newClass = new Class1();
Console.WriteLine(newClass.PrintY(9));
}
}
Both ways print out 9. Why should I use an object method and then create an instance of it?!
If you have to ask the question, then you shouldn't (use an object method). However, if you create two classes, they will affect each other.
Class1 newClassA = new Class1();
Class1 newClassB = new Class1();
Console.WriteLine(newClassA.PrintY(1));
Console.WriteLine(newClassB.PrintY(9));
After this code, newClassA's y is 9.
Related
I have three classes and I would like to be able to call static functions from the returned Class<Access>. I would like to select class type based on conditions.
class Access {
public static function get(item: Int): Int { return -1; }
public static function getAccessType(): Class<Access> {
if(Client.hasConnection())
return Remote;
else return Local;
}
}
class Remote extends Access {
override public static function get(item: Int): Int { return Server.getItem(item); }
}
class Local extends Access {
override public static function get(item: Int): Int { return Client.getItem(item); }
}
You can't override a static function in Haxe.
But you can probably achieve what you're trying to do by simply removing the override in Remote and Local
Can be done with singletons.
However, still the question might relevant whether such feature in Haxe even exists.
Depending on target, you may be able to cast a class to an interface/typedef to pull out values in a type-safe-ish way. "override" does not work for static methods
class Test {
static function pick(z:Bool):HasGetItem {
return z ? cast A : cast B;
}
static function main() {
trace("Haxe is great!");
trace(pick(false).getItem(1));
trace(pick(true).getItem(2));
}
}
#:keep class A {
public static function getItem(i:Int):Int return 10;
}
#:keep class B {
public static function getItem(i:Int):Int return 5;
}
typedef HasGetItem = {
getItem:Int->Int
}
https://try.haxe.org/#b2b87
How can I define class methods OUTSIDE of the class' brackets to improve readability? E.g., something similar to this...
class MyClass {
//Private variables:
int _foo = 0;
//Private methods:
void _incrementFoo();
//Public methods:
int getFoo();
}
void _incrementFoo() {
_foo++;
}
int getFoo() {
_incrementFoo();
return _foo--;
}
As you can see, I would like to tell the class about the methods by providing a signature, then I would actually define them outside of the class... Any way to do this in dart?
Well, I don't agree that what you want to do would improve the readability. If you want to split the interface and implementation I think the following are a lot better:
abstract class MyClass {
//Public methods:
int getFoo();
factory MyClass() => _MyClass();
}
class _MyClass implements MyClass {
//Private variables:
int _foo = 0;
//Private methods:
void _incrementFoo() => _foo++;
//Public methods implementation:
int getFoo() => _foo;
}
If you really want to do it you way you need to have a way to tell you method that they are part of you class. You could do something like this:
class MyClass {
//Private variables:
int _foo = 0;
//Private methods:
void _incrementFoo() => _incrementFooImpl(this);
//Public methods:
int getFoo() => getFooImpl(this);
}
void _incrementFooImpl(MyClass myClass) {
myClass._foo++;
}
int getFooImpl(MyClass myClass) {
myClass._incrementFoo();
return myClass._foo--;
}
But I think this is rather ugly and you are ending up adding a lot of global methods in your code which can make it hard to see which class each method are belonging to.
I know this is a basic question but I really struggle with this :(
First class:
public class A{
C c= new C();
B b= new B();
public static void main(String[] args) {
b.start();
System.out.println(c.getSomething());
}
}
Second Class:
public class B{
C c= new C();
public void start(){
c.setSomething(2);
}
}
Third Class:
public class C{
int x;
public int getSomething() {
return x;
}
public void setSomething(int x) {
this.x = x;
}
}
Now I know I make a new object in class A thats why the sysout returns null.
How can I make it so that I GET the value 2 instead of null in class A, and that I'm able to SET things in class B.
So I stay at the same object so to say.
I just want to be able to SET things in class B and GET that same value from the setter-getter-class-C, in class A. Please help
Thanks in advance, Jimme
Let's say we have two C++/CLI interfaces declaring Foo() members with different return type.
public interface class InterfaceA
{
bool Foo();
};
public interface class InterfaceB
{
int Foo();
};
What we want to do here is to have a class that instantiates an object that can be accessed through the above interfaces. So, the straight forward way to do that would be:
public ref class Class : InterfaceA, InterfaceB
{
virtual bool Foo() = InterfaceA::Foo { return true; }
virtual int Foo() = InterfaceB::Foo { return 10; }
};
Unfortunately that gives us compiler error "overloaded function differs only by return type from". Is there any workaround for this C++/CLI limitation?
No, you have to rename the method. For example:
public ref class Class : InterfaceA, InterfaceB
{
public:
virtual bool Foo() { return true; }
virtual int Foo2() = InterfaceB::Foo { return 10; }
};
Note how this is never a real problem. If code has a reference to Class instead of the interface for some reason then it can always call InterfaceB::Foo() with a cast:
Class^ obj = gcnew Class;
safe_cast<InterfaceB^>(obj)->Foo();
(This is .Net 3.5) I have a class FooList which implements IList and a class FooClass which implements IFoo. A user requires IList<IFoo>. In my implementation, I create a FooList<FooClass>, called X. How do I code my return so that my FooList<FooClass> X becomes his IList<IFoo>?
If I try
return X.Cast( ).ToList( );
he gets an IList<IFoo>, but it is not my FooList; it is a List, and a new one at that.
This isn't going to work out, because a FooList<FooClass> is not an IList<IFoo>. This is why:
var myList = new FooList<FooClass>();
IFoo obj = new SomeOtherFooClass();
IList<IFoo> result = (IList<IFoo>)myList; // hypothetical, wouldn't actually work
result.Add(obj); // uh-oh, now myList has SomeOtherFooClass
You need to either make a copy or use an interface that is actually covariant on the contained type, like IEnumerable<T> instead of IList<T>. Or, if appropriate, you should declare your FooList<FooClass> as an FooList<IFoo> from the get-go instead.
Here is a small implementation that demonstrates my second suggestion:
public interface IFoo { }
public class FooClass : IFoo { }
public class FooList<T> : IList<T>
{
public void RemoveAt(int index) { /* ... */ }
/* further boring implementation of IList<T> goes here */
}
public static void ListConsumer(IList<IFoo> foos)
{
foos.RemoveAt(0); // or whatever
}
public static IList<IFoo> ListProducer()
{
// FooList<FooClass> foos = new FooList<FooClass>(); // would not work
FooList<IFoo> foos = new FooList<IFoo>();
foos.Add(new FooClass());
return foos; // a FooList<IFoo> is an IList<IFoo> so this is cool
}
public static void Demo()
{
ListConsumer(ListProducer()); // no problemo
}