CS#61B#Data#Structures#and#Programming#Methodology##June%24%2008%David%Sun%Announcements#• Ben’s#Office#Hours:##– Monday%4‐5pm,%Tuesday%2‐3pm%in%Soda%611.%• Need#a#thorough#set#of#notes#for#this#class##– $160%for%the%entire%semester.%– Quality%and%speed%counts.%– If%interested,%email%me%with%your%notes%for%today%and%yesterday%@%[email protected]%From#last#time…#Class#with#a#single#main#method#public class FirstApp { public static void main(String[] args){ String[] greeting = new String[3]; greeting[0] = "Welcome to CS 61b"; greeting[1] = "from David, Ben, Adam"; greeting[2] = "and George"; for (String g: greeting){ System.out.println(g); } } }Today#• Object#oriented#programming#• Defining#Classes#• Constructors#• Access#Privileges#Object#Oriented#Programming#Classes#and#Objects#• A#class#is#a#template#or#blueprint#from#which#you#construct#objects.#• A#constructed#object#is#an#instance+of#a#class#• The#standard#Java#library#supplies#thousands#of#classes.#• You#still#need#to#create#your#own#to#describe#the#objects#of#the#problem#domain#of#your#application.##Class ObjectEncapsulation#(data#hiding)#• Combining#data#and#behavior#in#one#package.#• Data#=#Instance+fields/instance+variables+• Behavior#=##Methods+• An#object’s#current#values#for#the#instance#variables#=#State+of+the+object+• Programs#only#interact#with#object#through#the#object’s#methods,#never#directly#through#the#object’s#instance+variables.#=%reuse%and%reliability%Objects#• Key#characteristics:#– Object’s%Behavior:%what%can%you%do%with%the%object,%what%methods%can%you%call?%– Object’s%State:%how%does%the%object%react%to%methods%applied%to%it?%What%does%it%look%like?%– Object’s%Identity:%how%to%distinguish%the%object%from%others%with%same%behavior%and%state?%• All#objects#that#are#instances#of#the#same#class#support#the#same+behavior.#• Each#object#stores#information#about#its#current#state.##• Identity#issue#covered#future#lectures#Defining#Classes#A#Minimal#Class#class Minimalist { } class MinimalistTest { public static void main(String[] args) { Minimalist mini = new Minimalist(); } }A#Human#Class#Age%Name%“I’m%name%and%I’m%age&years%old!”%class Human { public int age; public String name; } class HumanTest { public static void main(String[] args) { Human wendy = new Human(); wendy.age = 12; wendy.name = “Wendy”; System.out.println(“I’m “ + wendy.name + “ and I’m “ + wendy.age + “ years old.”). } } What+about+encapsulation??+A#(Better)#Class#Definition#class Human { private int age; private String name; public Human(int a, String n) { age = a; name = n; } public void introduce() { System.out.println(“I’m “ + name + “ and I’m “ + age + “ years old.”). } } Write&code&to&initialize&the&object.&class HumanTest { public static void main(String[] args) { Human wendy = new Human(12, “Wendy”); wendy.introduce(); //”I’am Wendy and I’m 12 years old” } }public#v.s#private#class Human { public int age; public String name; } class Human { private int age; private String name; … } public :#any#method#in#any#class#has#access. class HumanTest{ public static void main(String[] args){ … wendy.age = 12; … } } private:#only#methods#that#can#access#the#field#are#methods#defined#with#in#the#class.### public void introduce() { System.out.println(“I’m “ + name + “ and I’m “ + age + “ years old.”). }#public#Fields#• You#could#use#public#keyword#with#fields#• Bad#idea#– Any%part%of%the%program%can%read%and%modif y%the%instance%field.%– Ruins%encapsulation.%%Constructors#public Human(int a, String n) { age = a; name = n; } • Constructor#has#the#same#name#as#the#class.#• Constructor#can#only#be#called#in#conjunction#with#the#new#operator.#– wendy.Human(21, “wendy”); //error • A#class#can#have#more#than#one#constructor#• A#constructor#can#take#zero#or#more#parameters#• A#constructor#has#no#return#value.##class Human { public int age; public String name; } class HumanTest { public static void main(String[] args) { Human wendy = new Human(); wendy.age = 12; wendy.name = “Wendy”; System.out.println(“I’m “ + wendy.name + “ and I’m “ + wendy.age + “ years old.”). } }Default#Constructor#• If#a#class#is#not#defined#with#a#constructor,#Java#provides#a#default+constructor#– Takes%no%parameter.%• A#default#constructor#can#be#explicitly#defined:#public Human() { } • If#you#defined#any+constructor,#the#default#constructor#provided#by#Java#goes#away.#– To%have%both%the%default%and%other%constructors,%you%must%define%both%explicitly.%%• Overriding#a#default#constructor:# public Human() { age = 0; name = “no name”; }Field#Initialization#• The#fields#are#initialized#to#their#default#values#in#the#default#constructor:# age = 0 name = null; • It’s#a#good#idea#to#always#set#the#values#of#the#fields#to#some#default#value:#class Human { private int age = -1; private String name = “”; … }Parameter#Names#public Human(int a, String n) { age = a; name = n; } public Human(int age , String name) { age = age ; name = name; } public Human(int aAge, String aName) { age = aAge; name = aName; } Parameter%variables%shadow%instance%fields.%%Must%read%the%program%to%decipher%the%meaning%of%the%parameters%this#Keyword#class Human { … public void introduce() { System.out.println(“I’m “ + name + “ and I’m “ + age + “ years old.”). } } class HumanTest { public static void main(String[] args) { Human wendy = new Human(12, “Wendy”); wendy.introduce(); //”I’am Wendy and I’m 12 years old” } } • An+implicit#parameter#precedes#the#field#instance.##• The#implicit#parameter#can#be#explicitly+referred#to#by#the#keyword this. System.out.println(“I’m “ + this.name + “ and I’m “ + this.age + “ years old.”).&wendy.name% wendy.age%Revisit#Parameter#Names##public Human(int a, String n) { age = a; name = n; } public Human(int aAge, String aName) { age = aAge; name = aName; } public Human(int age, String name) { this.age = age; this.name = name;
View Full Document