Comp 110 Object Construction

We have a class of objects! Now how do we make on?

After we have defined a class, we know what that object will look like once it's created, but first it needs to be constructed! (We know what strings and numbers do in theory, but until they are declared and initialized, we can't do anything with them!)

Lets use an example:

class Mascot {
     name: string = "?";
     school: string = "?";
     species: string = "?";
     yearBorn: number = 0;

According to this class definition, we are able to create 'Mascot' objects. Each mascot, on construction, will have default properties: 'name' "?", 'school' "?", 'species' "?", and 'yearBorn' 0...So let's make some mascots!

let rameses: Mascot;
//a new object 'rameses' is declared

rameses = new Mascot();
//a new mascot is constructed!

let theEnemy = new Mascot();

...but right now we dont know anything about Rameses! will give us "?"...

so lets assign Rameses' properties! = "RAMESES"; = "UNC";
rameses.species = "Ram";
rameses.yearBorn = 1789; = "The Blue Deveil"; = "UNC";
theEnemy.species = "Ram";
theEnemy.yearBorn = 1789;

Check out the pages on Classes and Properties for more details on how this works!

Now we have fully created a couple Mascot objects, we can not use and manipulate them!