6.092-5 Access control, class scope, packages, Java API
� ������������������������������������������������������������ ���������� ������ � �������������� � ����������� � �������� � �������� � � � � � ���������������������� ���������������� ������������������������ ������������������ ���������� ����������� ���������������������������������������� ��������������������������������� ��������������������������������� ��������������������� ��������������������� ��������������������� ����������������������������������� ���������������������������������������������������������������������������������������������������������������������������� � � � ���������������������������������������������������������������� ��������������������������������������� ���������������� ������������������������ ������������������ ���������� ����������� � ���������������������������������������� ��������������������������������� ��������������������������������� ��������������������� ��������������������� ��������������������� ����������������������������������� ���������������������������������������������������������������������������������������������������������������������������� �������������� ��������������������������� ����������� ���������������������������������� �������������� ��������������� ��������������������������� ����������� ����������� ������������������������������������������������������������������� �������������� ��������������� ��������������������������� ����������� ����������� � � ��������������������������������� ��������������������������������� ��������������������� � � ������������� ��������������� ��������������������������� ����������� ����������� � �� � ��������������������������������� ��������������������������������� ��������������������� ��������������������� � � ������������� ��������������� ��������������������������� ����������� ����������� � �� � � � ��������������������������������� ��������������������������������� ��������������������� ��������������������� ��������������������� � ���������� ������ � �������������� � ����������� � �������� � �������� � � � � � � ��������������������������������������� ������������������ ���������������� ���������������������������� ����������������������������� ��������������������������������������� ������������������������������������ ������������������ ������������������ � ������������public class Malicious { public static void main(String[] args) { maliciousMethod(new CreditCard()); } static void maliciousMethod(CreditCard card) { card.expenses = 0; System.out.println(card.cardNumber); } } � �������������������� ��������������������������� � ������������������������������������ ��������������������������������������������� � � � � � ��������������������������������������� ������������������ ���������������� ���������������������������� ����������������������������� ��������������������������������������� ������������������������������������ ������������������ ������������������ � � � � ��� � � �������������������������������������������������� �������������������������� ������������������������ ����������������������������������� ����������������������������� ��������������������������������������������� ������������������������������������ ������������������ ������������������ � �������������������� ����������������������������������� � ���������������������������������������� � ������������������������������������������� � ���������� ������ � �������������� � ����������� � �������� � �������� � �������������public class ScopeReview { var2 = "above 0";} else {var2 = "less than or equal to 0"; } System.out.println(var2); void scopeMethod(int var1) {String var2;if (var1>0) {} } � � ������������ public class ScopeReview { private int var3; void scopeMethod(int var1) { var3 = var1; String var2;if (var1>0) {var2 = "above 0";} else {var2 = "less than or equal to 0"; } System.out.println(var2); } } � � �����������public class ScopeReview { var3 = var1; private int var3; void scopeMethod(int var1) { String var2;if (var1> 0) {var2 = "above 0";} else {var2 = "less than or equal to 0"; } System.out.println(var2); } } � �� �������������������������������������������������� ��������� � ������������������������������������ ����������������������� ������������������������ � ��������������������������������������������������������������������������������������������������������������������������������������������� � ���������������������������������������public class Baby { int servings; void feed(int servings) { servings = servings + servings; } void poop() { System.out.println("All better!"); servings = 0; } } � ����� � ��������������� ��������������� � ����������������� ������ ��������������� ���������������������� ��������������������������������� ������������������������������������� � � ���������������������������������������public class Baby { int servings; void feed(int servings) { servings = servings + servings; } void poop() { System.out.println("All better!"); servings = 0; } } � � ����������������������������������public class Baby { int servings; void feed(int servings) { this.servings = this.servings + servings;}void poop() {System.out.println("All better!"); servings = 0; } } � ���������� ������ � �������������� � ����������� � �������� � �������� Packages � Each class belongs to a package � Classes in the same package serve a similar purpose � Packages are just directories � Classes in other packages need to be imported ���� � ����������������� ��������������������������������������� �������������� ������������������������������� ����������������������������� package parenttools; public class BabyFood { } package parenttools;public class Baby { } package adult; import parenttools.Baby; import parenttools.BabyFood; public class Parent { public static void main(String[] args) { Baby baby = new Baby(); baby.feed(new BabyFood()); } } Eclipse Demo������������� � Combine similar functionality � org.boston.libraries.Library � org.boston.libraries.Book � Separate similar names � shopping.List � packing.List Special PackagesAll classes “see” classes in the same package (no import needed) All classes “see” classes in java.lang Example: java.lang.String; java.lang.System � ���������� ������ � �������������� � ����������� � �������� � �������� Java APIJava includes lots of packages/classes Reuse classes to avoid extra work http://java.sun.com/javase/6/docs/api/Arrays with items Create the array bigger than you need Track the next “available” slot Book[] books = new Book[10]; int nextIndex = 0; books[nextIndex] = b; nextIndex = nextIndex + 1; Arrays with items Create the array bigger than you need Track the next “available” slot Book[] books = new Book[10]; int nextIndex = 0; books[nextIndex] = b; nextIndex = nextIndex + 1; ����������������������������� � � � ArrayListModifiable list Internally implemented with arrays Features Get/put items by index Add items Delete items Loop over all items Array � ArrayListBook[] books = new Book[10]; int nextIndex = 0; books[nextIndex] = b; nextIndex += 1; ArrayList books = new ArrayList(); books.add(b); import java.util.ArrayList;class ArrayListExample {public static void main(String[] arguments){ArrayList strings = new ArrayList();strings.add("Evan");strings.add("Eugene");strings.add("Adam");System.out.println(strings.size());System.out.println(strings.get(0));System.out.println(strings.get(1));strings.set(0, "Goodbye");strings.remove(1);������������������������������������������ �������� ����������������������������������� ����� for (String s : strings){ System.out.println(s); } } } � � � � � Sets Like an ArrayList, but Only one copy of each object, and No array index Features Add objects to the set Remove objects from the set Is an object in the set? TreeSet: Sorted (lowest to highest) HashSet: Unordered (pseudo-random) import java.util.TreeSet; class SetExample { public static void main(String[] arguments){ TreeSet strings = new TreeSet(); strings.add("Evan"); strings.add("Eugene"); strings.add("Adam"); System.out.println(strings.size());System.out.println(strings.first());System.out.println(strings.last());strings.remove("Eugene"); for (String s : strings){ System.out.println(s); } } } Maps Stores a (key, value) pair of objectsLook up the key, get back the valueExample: Address Book � Map from names to email addresses TreeMap: Sorted (lowest to highest) HashMap: Unordered (pseudo-random) public static void main(String[] arguments){ HashMap strings = new HashMap(); strings.put("Evan", "email1@mit.edu"); strings.put("Eugene", "email2@mit.edu"); strings.put("Adam", "email3@mit.edu”); System.out.println(strings.size());strings.remove("Evan");System.out.println(strings.get("Eugene"));for (String s : strings.keySet()) { System.out.println(s);}for (String s : strings.values()) {System.out.println(s);}for (Map.Entry pairs : strings.entrySet()) {System.out.println(pairs); } } WarningsUsing TreeSet/TreeMap? Read about Comparable interface Using HashSet/HashMap? Read about equals, hashCode methods Note: This only matters for classes you build, not for java built-in types. � ��������� ������ � �������������� � ����������� � �������� � �������� � ���������������������� ����������������������������������������������� ������������� � ������������������������������������������������� ������������� ����� ����� ������ ����� �����MIT OpenCourseWarehttp://ocw.mit.edu 6.092 Introduction to Programming in Java January (IAP) 2010 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Description
This lecture notes introduces Access control, class scope, packages, Java API . Acces control protect private information .This key word also explained here. Each class belongs to a package. Combine similar functionality with the help of package.Java includes lots of packages / classes Reuse classes to avoid extra work.
“Level:Undergraduate Instructors:Evan Jones,Adam Marcus,Eugene Wu ,6.092-5 Access control, class scope, packages, Java API 6.092 Introduction to Programming in Java,Electrical Engineering and Computer Science, Engineering, Massachusetts Institute of Technology: MIT Open Course Ware,http://ocw.mit.edu (30-08-2011).License: Creative Commons BY-NC-SA: http://ocw.mit.edu/terms/#cc".
Presentation Transcript
Your Facebook Friends on WizIQ