Содержание
- 2. Agenda Nested Classes Non-static Nested Classes Static Nested Classes Local Inner Classes Anonymous Inner Class Object
- 3. Nested Classes In Java, just like methods, variables of a class too can have another class
- 4. Non-static Nested Classes Inner classes are a security mechanism in Java. public class Person { private
- 5. Non-static Nested Classes In the given example, we make the inner class private and access the
- 6. Non-static Nested Classes If inner class isn't private you can instantiate it ! public class Person
- 7. Non-static Nested Classes To instantiate the inner class, initially you have to instantiate the outer class.
- 8. Static Nested Classes A static inner class is a nested class which is a static member
- 9. Static Nested Classes If you compile and execute the above program, you will get the following
- 10. Local Inner Classes A local inner class can be instantiated only within the method where it
- 11. Anonymous Inner Class Anonymous inner class is a class that has no name and is used
- 12. Anonymous Inner Class In this case: operator new creates an object. The Comparator() begins definition of
- 13. Default Methods for Interfaces Java 8 enables us to add non-abstract method implementations to interfaces by
- 14. Anonymous Class and Default Methods Interface Formula defines a default method sqrt() which can be accessed
- 15. Objects Cloning Object Cloning is a process of generating the exact field-to-field copy of object with
- 16. The clone() method Example: public class Person { private FullName fullName; private int age; public Person(FullName
- 17. The Cloneable Interface The program would throw CloneNotSupportedException if we don’t implement the Cloneable interface. A
- 18. The Cloneable Interface Example: Person person = new Person(new FullName("Mike", "Green"), 25); Person copyOfPerson = (Person)
- 19. Deep Copy vs Shallow Copy Shallow copy is “default implementation” in Java and if you are
- 20. Deep Copy Example: class FullName implements Cloneable { // ... @Override protected Object clone() throws CloneNotSupportedException
- 21. Wrapper Pattern Non-generic Box class public class Box { private Object obj; public void set(Object obj)
- 22. Wrapper Pattern public class Appl { public static void main(String[ ] args) { String text =
- 23. Wrapper Pattern Wrapper (or Decorator) is one of the most important design patterns. One class takes
- 24. Wrapper Pattern public class Appl { public static void main(String[ ] args) { String text =
- 25. Generic in Java Generics, introduced in Java SE 5.0 A generic type is a generic class
- 26. Generic in Java To update the Box class to use generics, you create a generic type
- 27. Generic in Java public class Box { // T stands for "Type". private T t; public
- 28. Generic in Java public class Appl { public static void main(String[ ] args) { String text
- 29. Type Parameter Naming Conventions The most commonly used type parameter names are: E – element (used
- 30. Generic in Java Java method can be parametrized, too: getRandomElement(List list) { … } As with
- 31. Bounded Arguments Consider a simple drawing application to draw shapes (circles, rectangles, …) Canvas draw(Shape shape)
- 32. Bounded Arguments A List of any kind of Shape … Shape is the upper bound of
- 33. Template Arguments class Box { T[] nums; Box(T[] о) { nums = о; } double average()
- 34. Template Arguments The “collection of unknown” is a collection whose element type matches anything – template
- 35. More fun with Generics public void pushAll(Collection collection) { for (E element : collection) { this.push(element);
- 36. Generic in Java Disadvantages Generic-fields can not be static. Static methods can not have generic parameters
- 37. Wrapper Classes The wrapper classes are objects encapsulating primitive Java types. Each Java primitive has a
- 38. Autoboxing and Unboxing After Java 5 the conversion primitive value to a wrapper object and from
- 39. Practical part Suppose we have the class Car. Create public inner class CarBuilder inside of Car
- 40. Practical part 2. Suppose we have the next diagram Create Wrapper class which should wrap any
- 41. Homework Task 1 Develop a FullName class with the firstName and lastName fields of type String,
- 42. Homework Develop a Student class with an int field that matches the course the student is
- 43. Homework Task 2 Create Wrapper class with private field of T type which is called value.
- 45. Скачать презентацию