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();
Yes you can do
MyObject hello = new AnotherObject();
MyObject hello = new AnotherObject()
if AnotherObject is a subclass of MyObject.
The main polymorphism. You can say myObject hello = new derivesFromMyObject ().
/ , , - :
BaseObject A = new ChildObject();
?
, :
, .
, , , . polymorphism. :
Vehicle a = new Car(); Vehicle b = new Bike();
. OO , , (cfr. ) .
, ( var) :
var
var someClass = new SomeClass();
, , , , :
BaseClass b = new ChildClass();
MyInterface i = new ClassThatImplementsInterface();
#.
. . , .
var , , :
var hello = new myObject();
.
, - , (, ), A a = new(); ...
var, , var , .
class Base {} class SubA : Base {} class SubB : Base {}
, , Base, SubA SubB
Base
SubA
SubB
SubA hello = new SubA(); hello = new SubB();
"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.
Source: https://habr.com/ru/post/1788485/More articles:Create full-text decimal / coloumn pointer, maybe ????? (SQL Server 2008) - sql-serverWhy does the factory pattern work the way it does? - design-patternsUsing an STL card for 16 GB of data on a 64-bit machine - c ++YouTube API PHP - how or don't like the video after authentication - youtube-apiLabVIEW: to have several indicators (different styles) for one variable - labviewYouTube API v3: watching videos in Python - pythonPrevent spam using artificial intelligence - artificial-intelligenceCannot start or jai_imageio or ImageIO.getImageReadersByFormatName cannot get an object - javaLinq to XML - multiple elements in one class - c #vim: insertion problem: reassignments (imap) and abbreviations (ab) in .vimrc do not work - vimAll Articles