1 / 29

Structuring Models

Structuring Models. CS1316: Representing Structure and Behavior. Story. Exploring classes, superclasses/subclasses, constructors, Strings, and println Let’s make a Person People have names Let’s make a Student a kind-of Person Students have ID numbers.

jabari
Télécharger la présentation

Structuring Models

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structuring Models CS1316: Representing Structure and Behavior

  2. Story • Exploring classes, superclasses/subclasses, constructors, Strings, and println • Let’s make a Person • People have names • Let’s make a Student a kind-of Person • Students have ID numbers. • Setting them new objects automatically • How inheritance works, and how overriding works

  3. People have names public class Person { public String name; } > Person fred = new Person(); > fred.name null > fred.name = "Fred"; > fred.name "Fred"

  4. Should we be able to rename Fred? > fred.name = "Mabel"; > fred.name "Mabel" This is the what public instance variables (sometimes called fields) allow us to do?

  5. If we wanted to control names, can do it in accessors > Person fred = new Person(); > fred.setName("Fred"); > fred.getName() "Fred" public class Person { private String name; public void setName(String somename) {this.name = somename;} public String getName() {return this.name;} } Could check in setName(): “Are you allowed to change my name?” Could check in getName(): “Are you allowed to ask my name?”

  6. Barney may not be bright,but is he really null? • > Person barney = new Person(); • > barney.getName() • null How could we set up a person’s name right from the start?

  7. Making new people have reasonable names public class Person { private String name; public Person(){ this.setName("Not-yet-named"); } public void setName(String somename) {this.name = somename;} public String getName() {return this.name;} } > Person barney = new Person() > barney.getName() "Not-yet-named"

  8. The Constructor • A method with the same name as the class. • It’s called when a new instance of the class is created. public Person(){ this.setName("Not-yet-named"); }

  9. Constructors can take inputs (arguments) public class Person { private String name; public Person(){ this.setName("Not-yet-named"); } public Person(String name){ this.setName(name); } public void setName(String somename) {this.name = somename;} public String getName() {return this.name;} } > Person barney = new Person("Barney") > barney.getName() "Barney" > Person wilma = new Person() > wilma.getName() "Not-yet-named" > wilma.setName("Wilma") > wilma.getName() "Wilma" Both constructors exist. Java uses the right one depending on the arguments provided.

  10. Must match inputs to some constructor > Person agent99 = new Person(99) java.lang.NoSuchMethodException: Person constructor There are constructors for no inputs and String inputs, but not for double or int inputs.

  11. Some discourse rules for Java • In a Western, the hero carries a gun, never a bazooka and never flies on a unicorn. • There are “discourse rules” in different genres. • There are such rules in Java. • Class names start with capital letters. • They are never plurals. (Want more than one of them? Make an array or a list!) • Instance variables and methods always start with lowercase letters. • Methods should describe verbs—what objects know how to do. • Accessors are typically set- and get- the name of the field.

  12. But what is Barney? > Person barney = new Person("Barney") > barney Person@63a721 Looks like “Person” then cuss word…

  13. Making the printable representation of People better public class Person { private String name; public String toString(){ return "Person named "+this.name; } toString() is called whenever an object needs to be converted to a string—such as when it is printed in the Interactions Pane > Person barney = new Person("Barney") > barney Person named Barney

  14. Students are people, too. • Let’s make Students • They are Persons • But they also have ID#s • (Not exactly the best model in the world. Real students also have homes, friends, etc. But you only model what you need, and right now, we just need another data element.) • Students are kind-of Persons • Students extend Persons

  15. Making Students public class Student extends Person { private int idnum; public int getID(){ return idnum;} public void setID(int id) {idnum = id;} }

  16. Making Betsy > Student betsy=new Student("Betsy") java.lang.NoSuchMethodException: Student constructor > Student betsy = new Student() > betsy.getName() "Not-yet-named" > betsy.setName("Betsy") > betsy.getName() "Betsy" > betsy.setID(999) > betsy.getID() 999 Can’t make Betsy by name, because that only exists for Persons so far.Betsy does inherit the existing constructor for no input. Betsy does inherit setName and getName. And Betsy now has getID and setID

  17. Who is Betsy? > betsy Person named Betsy Students inherit the toString() of the parent class.

  18. Setting up Student constructors public class Student extends Person { private int idnum; // Constructors public Student(){ super(); //Call the parent's constructor idnum = -1; } public Student(String name){ super(name); idnum = -1; } public int getID(){ return idnum;} public void setID(int id) {idnum = id;} } If a Student has no ID, the default now is -1. If we want to call the parent’s constructor, we must do it in the FIRST line.

  19. Your turn: Actually try these in class • Make a new constructor for Student that takes a name and an ID, so that we can say:Student mark = new Student(“Mark”,29) • Make a toString() for Students so that they respond with name and ID.

  20. Can we have a main() here? • Any class can have a public static void main(String[] args) method. • What would you use it for? • How about for testing? • Use it like we use the Interactions Pane

  21. Getting values printed out • How do we get things printed to the Interactions Pane from inside a main()?System.out.println(“Some String”)

  22. A main() for Persons public static void main(String [] args){ Person fred = new Person("Fred"); Person barney = new Person("Barney"); System.out.println("Fred is "+fred); System.out.println("Barney is "+barney); } As long as you start with a string, you can “add” (with “+”) any object and it will be made into a string. Welcome to DrJava. > java Person Fred is Person named Fred Barney is Person named Barney

  23. The whole Person.java public class Person { private String name; public Person(){ this.setName("Not-yet-named"); } public Person(String name){ this.setName(name); } public String toString(){ return "Person named "+this.name; } public void setName(String somename) {this.name = somename;} public String getName() {return this.name;} public static void main(String [] args){ Person fred = new Person("Fred"); Person barney = new Person("Barney"); System.out.println("Fred is "+fred); System.out.println("Barney is "+barney); } }

  24. Exploring greetings • A subclass inherits all structure (data) and behavior (methods) from superclass. • Unless it overrides it.

  25. A new method for Person public void greet(){ System.out.println("Hi! I am "+this.name); } > Person bruce = new Person("Bruce") > bruce.greet() Hi! I am Bruce > Person george = new Person("George W.") > george.greet() Hi! I am George W. > Student krista = new Student("Krista") > krista.greet() Hi! I am Krista Should students greet() in the same way?

  26. Trying to give Student a greet()

  27. This way works public void greet(){ System.out.println("Hi, I'm "+this.getName()+ ", but I got to run to class..."); }

  28. Changes Student, but not Person • Welcome to DrJava. • > Person george = new Person("George W.") • > george.greet() • Hi! I am George W. • > Student krista = new Student("Krista") • > krista.greet() • Hi, I'm Krista, but I got to run to class...

  29. Think about what happens here • When we tell krista to greet() • “I am a Student. Do I know how to greet()? If I don’t, I’ll ask my parent, Person, to handle it.” • “I do know how to greet()! I’ll execute that method!” • “Uh-oh. I don’t know how to getName(). Oh, Parent?!? Can you handle getName()?” public void greet(){ System.out.println("Hi, I'm "+this.getName()+ ", but I got to run to class..."); }

More Related