SLIDE 10 9/4/20 10
Constructor: Chicken.java
Sep 4, 2020 Sprenkle - CSCI209 19
public public class class Chicken { Chicken { // --------- INSTANCE VARIABLES --------------- private String private String name; name; private private int int height; height; // in cm private double private double weight; weight; // --------- CONSTRUCTORS --------------- public public Chicken( Chicken(String name, int int h, h, double double weight) { weight) { this this.name name = name; = name; this this.height height = h; = h; this this.weight weight = weight; = weight; } …
Observations? Thoughts? Questions?
19
Constructor: Chicken.java
Sep 4, 2020 Sprenkle - CSCI209 20
public public class class Chicken { Chicken { // --------- INSTANCE VARIABLES --------------- private String private String name; name; private private int int height; height; // in cm private double private double weight; weight; // --------- CONSTRUCTORS --------------- public public Chicken( Chicken(String name, int int h, h, double double weight) { weight) { this this.name name = name; = name; this this.height height = h; = h; this this.weight weight = weight; = weight; } …
this this: Special name for the constructed object, like self in Python (differentiate from parameters) Type and name for each parameter Constructor name same as class’s name Params don’t need to be same names as instance var names
20