C # what type point = new type ()?

Given:

myObject hello = new myObject();

What is the point to point myObject again in the second part of the line of code?

Could you do something like:

myObject hello = new anotherObject();

Why not just

myObject hello = new();
+3
source share
9 answers

Yes you can do

MyObject hello = new AnotherObject();

if AnotherObject is a subclass of MyObject.

+3
source

The main polymorphism. You can say myObject hello = new derivesFromMyObject ().

+7
source

/ , , - :

BaseObject A = new ChildObject();
+4

?

, :

myObject hello = new();

, .

, , , . polymorphism. :

Vehicle a = new Car();
Vehicle b = new Bike();

. OO , , (cfr. ) .

+3

, ( var) :

var someClass = new SomeClass();

, , , , :

BaseClass b = new ChildClass();

MyInterface i = new ClassThatImplementsInterface();
+3

#.

. . , .

var , , :

var hello = new myObject();

, .

+1

.

, - , (, ), A a = new(); ...

var, , var , .

+1

var

var hello = new myObject();

, .

, .

class Base {}

class SubA : Base {}

class SubB : Base {}

, , Base, SubA SubB

SubA hello = new SubA();
hello = new SubB();
+1
myObject hello = new myObject();

"myObject hello" .

"new myObject ()" calls the constructor for myObject ... Perhaps you have several constructors that take different sets of parameters to create different flavors of the object.

0
source

Source: https://habr.com/ru/post/1788485/


All Articles