Skip to content

Latest commit

 

History

History
117 lines (88 loc) · 3.38 KB

File metadata and controls

117 lines (88 loc) · 3.38 KB
title Classes, the Basics
actions
checkAnswer
hints
material
editor
language startingCode answer
c#
public class AlienFinder : SmartContract { // enter answer here public static void Main (string alienName) { ulong randomNumber; uint xna; } }
public class AlienFinder : SmartContract { public class Alien { public uint xna; public string alienName; } public static void Main (string alienName) { ulong randomNumber; uint xna; Alien someAlien = new Alien (); } }

Tag: C# Basics

Now we need a data type that encapsulates the attributes of each Alien, and this is where classes come in. A class is a 'blueprint' for a type of object. It can have multiple variables of different data types, it can also have custom defined behaviours (methods, which we will cover later).

For now, we will focus on classes that simply have multiple variables.

Here is an example of a simple class:

public class Point {
    public int x;
    public int y;
}

The code above defines a new type of class called 'Point'. Here, each instance of 'Point' has two int fields: x-corrdinate and y-coordinate.

One can intialize an object of class Point, and modify its fields:

Point p = new Point (); 
p.x = 5; 
p.y = 6; 

The public keyword in front of x and y is an access modifier. It means any code can access and modify these variables. A private modifier makes a field only accessible by code in the same class.

Constructors

After defining the Alien class, we can specify how each instance of Alien is created by defining a constructor.

Below is an example of a constructor.

public class Point {
    public static int counter; 
    public int x;
    public int y;
    public Point (int x, int y) {
        this.x = x;
        this.y = y;
    }
}

public Point (int x, int y) {...} is the constructor for new 'Points'. It takes two int parameters and creates a 'Point', whose x is equal to the first parameter, and y equal to the second.

With the above class definition, we can create a new 'Point' like this:

var p1 = new Point (3, 5);
Point p2 = new Point (4, 6); // specifies the type of p2

Default Constructor

If Point does not have a constructor definition (as shown below), it would automaticallhy have a default constructor, which takes no parameters.

public class Point {
    public int x;
    public int y;
}

var p2 = new Point (); 

This creates an object where each field's data is their default value. (0, null or false. Base on what the type is)

Importantly, if the data inside the class is public, you can pass in arguments like this:

var p2 = new Point {
    x = 4,
    y = 6
}; 

This is a common practice for simple classes with public fields.

Instructions:

Define an Alien class that includes the three attributes we need for each 'Alien species': xna, name, and the block it was found on.

  • Define a class called Alien
  • Declare two public fields: a uint named xna and a string named alienName.
  • In the Main method, declare a Alien type called someAlien, call the default constructor without arguments.