SlideShare ist ein Scribd-Unternehmen logo
1 von 46
3. Классы
Программирование на Java
Федор Лаврентьев
МФТИ, 2016
Пакеты (packages)
Файл java/lang/Integer.java
package java.lang;
public class Integer {
public static final int MAX_VALUE = ...;
...
}
Файл ru/mipt/java/lection3/Example.java
package ru.mipt.java.lection3;
[import java.lang.Integer;]
import java.io.InputStream;
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.toHexString;
public class Example {
Integer maxInt = MAX_VALUE;
String maxString = toHexString(MAX_VALUE);
InputStream is =
new java.io.FileInputStream(“foo.txt”);
InputSteam is2 = new ru.mipt.java.lection3. FileInputStream();
}
Файлы и директории
ru/
- mipt/
- java/
|- lection3/
| |- Child.java
| |- Example.java
| - Parent.java
- lection4/
- Utility.java
java/
|- io/
| |- IOException.java
| - InputStream.java
- lang/
- String.java
Прямое наследование
Наследование
class Rectangle {
final double width, height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
}
class Square extends Rectangle {
Square(double side) {
super(side, side);
}
}
LSP (Barbara Liskov’s Substitution Principle)
double calcArea(Rectangle r) {
return r.getWidth() * r.getHeight();
}
Rectangle a = new Rectangle(4, 5);
calcArea(a); // 20
Rectangle b = new Square(4);
calcArea(b); // 16
Square c = new Square(5);
calcArea(c); // 25
Приведение и проверка типов
public void draw(Shape shape) {
if (shape instanceof Rectangle) {
Rectangle rect = (Rectangle) shape;
drawRectangle(rect);
} else if (shape instanceof Circle) {
Circle circle = (Circle) shape;
drawCircle(circle);
} else if (shape instanceof Line) {
drawLine((Line) shape);
}
}
Абстрактные классы
abstract class Shape {
abstract double calcArea();
}
class Rectangle extends Shape {
...
@Override
double calcArea() {
return getWidth() * getHeight();
}
}
Переопределение метода
class Object {
public int hashCode() { ... }
}
class Long [extends Object] {
private final long value;
@Override
public int hashCode() {
return (int) (value ^ (value >>> 32))
}
}
Вызов метода родительского класса
class Circle {
public void draw() {
Canvas.circle(x, y, radius);
}
}
class Sun extends Circle {
@Override
public void draw() {
super.draw(); // Draw circle
drawSunlight();
}
}
Инстанцирование абстрактного класса
Shape a = new Rectangle(3, 4);
a.calcArea(); // 12
Shape b = new Square(7);
b.calcArea(); // 49
Shape c = new Shape();
c.calcArea(); // ?
Инстанцирование абстрактного класса
Shape a = new Rectangle(3, 4);
a.calcArea(); // 12
Shape b = new Square(7);
b.calcArea(); // 49
Shape c = new Shape(); // WRONG!
c.calcArea();
Анонимные классы
Shape a = new Rectangle(3, 4);
a.calcArea(); // 12
Shape b = new Square(7);
b.calcArea(); // 49
Shape c = new Shape() {
double calcArea() { return 0; }
}
c.calcArea(); // 0
Модификаторы доступа
Инкапсуляция - private и public
public class Animal {
private double x;
private double y;
private double bearing;
public double getX() { return x; }
public double getY() { return y; }
public double getBearing() { return bearing; }
public void rotate(double angle) {
bearing = modulo(bearing + angle, 2 * Math.PI);
}
public void stepUp(double distance) {
x += distance * Math.cos(bearing);
y += distance * Math.sin(bearing);
}
}
Точки расширения - protected
public class Animal {
...
private boolean hungry;
public boolean isHungry() { return hungry;}
protected void setHungry(boolean hungry) {
this.hungry = hungry;
}
}
public class LazyAnimal {
@Override
public void stepUp(double distance) {
if (isHungry()) throw new IllegalStateException();
super.stepUp(distance);
setHungry(true);
}
}
Доступ по умолчанию (package-local)
package edu.phystech.java.lection3;
public class Animal {
...
String name;
}
public class Dog extends Animal {
public String tellName() { return “Bark!”; }
}
public class Student extends Animal {
public String tellName() { return name; }
}
public class Zookeeper {
public String nameAnimal(Animal a) {
return a.name;
}
}
Хорошая, годная инкапсуляция
public class Wallet {
private double money = 0.0;
public void putMoney(double amount) {
if (amount < 0) throw new IllegalArgumentException();
money += amount
}
public void takeMoney(double amount) {
if (amount < 0) throw new IllegalArgumentException();
if (amount > money) throw new IllegalArgumentException();
money -= amount;
}
public double getMoney() {
return money;
}
}
Нарушение инкапсуляции
public class Wallet {
public double money = 0.0;
public void putMoney(double amount) {
if (amount < 0) throw new IllegalArgumentException();
money += amount
}
public void takeMoney(double amount) {
if (amount < 0) throw new IllegalArgumentException();
if (amount > money) throw new IllegalArgumentException();
money -= amount;
}
public double getMoney() {
return money;
}
}
Public Morozoff в один ход
public class Wallet {
public double money;
...
}
public static void leakMoney(Wallet w) {
w.money = -5;
}
Возможное нарушение инкапсуляции
public class Wallet {
protected double money = 0.0;
public void putMoney(double amount) {
if (amount < 0) throw new IllegalArgumentException();
money += amount
}
public void takeMoney(double amount) {
if (amount < 0) throw new IllegalArgumentException();
if (amount > money) throw new IllegalArgumentException();
money -= amount;
}
public double getMoney() {
return money;
}
}
Public Morozoff в два хода
public class Wallet {
protected double money;
...
}
pubic class LeakingWallet extends Wallet {
public void setMoney(double amount) {
money = amount;
}
}
public void leakMoney(LeakingWallet w) {
w.setMoney(-5);
}
Внутренние классы
public class Dog {
private double bearing; // Направление
public class Head {
private final int length;
public String smash() {}
}
}
Вложенные классы
public class Observable {
public static class Event {
private final Observable source;
private final String type;
private final String cause;
}
public static class Observer {
void onEvent(Event e) { ... }
}
}
Множественное наследование
Интерфейсы
public interface Planar {
[public] void setLocation(Location l);
[public] Location getLocation();
}
public class Rectangle implements Planar {
@Override
void setLocation(Location l) {
this.left = l.getX();
this.top = l.getY();
}
@Override
void getLocation() {
return new Location(getLeft(), getTop());
}
}
Абстрактная имплементация интерфейса
public interface Planar {
void setLocation(Location l);
Location getLocation();
}
public abstract class Shape implements Planar {
// setLocation() unimplemented
// getLocation() unimplemented
}
public class Rectangle extends Shape {
...
}
Наследование интерфейсов
public interface Planar {
void setLocation(Location l);
Location getLocation();
}
public interface Drawable extends Planar {
void draw(Canvas c);
}
public class Shape implements Drawable {
...
}
Множественное наследование
public class Geometry {
...
}
public class Circle
extends Geometry
implements Drawable, Comparable, Closeable {
...
}
Реализация по умолчанию
public interface Observable {
List<Observer> getObservers();
void addObserver(Observer o) default {
getObservers().add(o);
}
Observer removeObserver(Observer o) default {
getObservers().remove(o);
}
void notifyObservers(Event e) default {
List<Observer> observers = getObservers();
for (int i = 0; i <= observers.size(); ++i) {
observers.get(i).onEvent(e);
}
}
}
Сравнение объектов
Сравнение по ссылке
Student s1 = new Student(“Vasya”, 21);
Student s2 = new Student(“Vasya”, 21);
Student s3 = s1;
assert s1 == s1; // true
assert s1 == s2; // false
assert s1 == s3; // true
Сравнение по ссылке
Integer a = 100500;
Integer b = Integer.valueOf(100500);
Integer c = Integer.valueOf(100500);
assert a == b;
assert a == c;
assert b == c;
Integer d = 42;
Integer e = Integer.valueOf(42)
assert d == e;
Сравнение по ссылке
Integer a = 100500;
Integer b = Integer.valueOf(100500);
Integer c = Integer.valueOf(100500);
assert a == b; // WRONG
assert a == c; // WRONG
assert b == c; // WRONG
Integer d = 42;
Integer e = Integer.valueOf(42)
assert d == e;
Сравнение по ссылке
Integer a = 100500;
Integer b = Integer.valueOf(100500);
Integer c = Integer.valueOf(100500);
assert a == b; // WRONG
assert a == c; // WRONG
assert b == c; // WRONG
Integer d = 42;
Integer e = Integer.valueOf(42)
assert d == e; // CORRECT?!
Сравнение объектов – equals
class Student {
...
public boolean equals(Object o) {
if (o == null) return false;
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
Student s = (Student) o;
if (this.age != s.age) return false;
if (this.name == null) return s.name == null;
return this.name.equals(s.name);
}
}
Сравнение объектов – equals (в Java 7)
class Student {
...
public boolean equals(Object o) {
if (o == null) return false;
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
Student s = (Student) o;
if (this.age != s.age) return false;
return Objects.equals(this.name, s.name);
}
}
Сравнение объектов – хеш-код
class Student {
...
public int hashCode() {
final int prime = 37;
return age * prime + name.hashCode();
}
}
Сравнение объектов – по умолчанию
public class Object {
...
public native int hashCode();
// адрес в памяти
public boolean equals(Object o) {
return this == o;
}
}
Контракт на сравнение объектов
• a.equals(a) == true; // Рефлексивность
• a.equals(b) == b.equals(a); // Симметричность
• a.equals(b) && b.equals(c) => a.equals(c); // Транзитивность
• a.equals(b) == a.equals(b); //Консистентность
• a.hashCode() == a.hashCode(); //Консистентность
• a.equals(b) == true => a.hashCode() == b.hashCode();
• a.equals(null) == false;
Enum
Enum
public enum TaskState {
QUEUED, RUNNING, FINISHED, FAILED;
}
import static TaskState.*;
public class Task {
private TaskState state;
void updateState(TaskState nextState) {
if (state == FINISHED || state == FAILED) {
throw new IllegalStateException();
}
if (state == RUNNING && nextState == QUEUED) {
throw new IllegalStateException();
}
state = nextState;
}
}
Расширение enum
public enum TaskState {
QUEUED(0), RUNNING(1), FINISHED(2), FAILED(2);
private final int order;
private TaskState(int order) {
this.order = order;
}
public boolean isUpdateAllowed(TaskState next) {
return next.order > this.order;
}
}
Расширение enum
import static TaskState.*;
public class Task {
private TaskState state;
void updateState(TaskState nextState) {
if (!state.isUpdateAllowed(nextState) {
throw new IllegalStateException();
}
state = nextState;
}
}

Weitere ähnliche Inhalte

Was ist angesagt?

Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Loïc Knuchel
 
C++14 reflections
C++14 reflections C++14 reflections
C++14 reflections corehard_by
 
Propuesta..sujei
Propuesta..sujeiPropuesta..sujei
Propuesta..sujeigersonjack
 
An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]Eleanor McHugh
 
Test driven java script development
Test driven java script developmentTest driven java script development
Test driven java script developmentStefan Scheidt
 
Антон Полухин. C++17
Антон Полухин. C++17Антон Полухин. C++17
Антон Полухин. C++17Sergey Platonov
 
Java AWT Calculadora
Java AWT CalculadoraJava AWT Calculadora
Java AWT Calculadorajubacalo
 
Java Thread Cronometro
Java Thread CronometroJava Thread Cronometro
Java Thread Cronometrojubacalo
 
Arrangement de la mémoire
Arrangement de la mémoireArrangement de la mémoire
Arrangement de la mémoireSalah Triki
 
Aller plus loin avec Doctrine2
Aller plus loin avec Doctrine2Aller plus loin avec Doctrine2
Aller plus loin avec Doctrine2André Tapia
 
Adi Fatol - Ce e nou in PHP 5.3?
Adi Fatol - Ce e nou in PHP 5.3?Adi Fatol - Ce e nou in PHP 5.3?
Adi Fatol - Ce e nou in PHP 5.3?phpGeekMeet_ro
 

Was ist angesagt? (19)

Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
 
C++14 reflections
C++14 reflections C++14 reflections
C++14 reflections
 
Propuesta..sujei
Propuesta..sujeiPropuesta..sujei
Propuesta..sujei
 
An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]
 
Test driven java script development
Test driven java script developmentTest driven java script development
Test driven java script development
 
Антон Полухин. C++17
Антон Полухин. C++17Антон Полухин. C++17
Антон Полухин. C++17
 
Java AWT Calculadora
Java AWT CalculadoraJava AWT Calculadora
Java AWT Calculadora
 
Taller de string(java)
Taller de string(java)Taller de string(java)
Taller de string(java)
 
Java Thread Cronometro
Java Thread CronometroJava Thread Cronometro
Java Thread Cronometro
 
Arrangement de la mémoire
Arrangement de la mémoireArrangement de la mémoire
Arrangement de la mémoire
 
es6.concurrency()
es6.concurrency()es6.concurrency()
es6.concurrency()
 
Groovy: il linguaggio
Groovy: il linguaggioGroovy: il linguaggio
Groovy: il linguaggio
 
Testování prakticky
Testování praktickyTestování prakticky
Testování prakticky
 
Aller plus loin avec Doctrine2
Aller plus loin avec Doctrine2Aller plus loin avec Doctrine2
Aller plus loin avec Doctrine2
 
Silex al límite
Silex al límiteSilex al límite
Silex al límite
 
Dapan
DapanDapan
Dapan
 
Adi Fatol - Ce e nou in PHP 5.3?
Adi Fatol - Ce e nou in PHP 5.3?Adi Fatol - Ce e nou in PHP 5.3?
Adi Fatol - Ce e nou in PHP 5.3?
 
Pague o aluguel
Pague o aluguelPague o aluguel
Pague o aluguel
 
Lab2 sdmp
Lab2 sdmpLab2 sdmp
Lab2 sdmp
 

Andere mochten auch

Programming Java - Lection 01 - Basics - Lavrentyev Fedor
Programming Java - Lection 01 - Basics - Lavrentyev FedorProgramming Java - Lection 01 - Basics - Lavrentyev Fedor
Programming Java - Lection 01 - Basics - Lavrentyev FedorFedor Lavrentyev
 
Programming Java - Lecture 02 - Objects - Lavrentyev Fedor
Programming Java - Lecture 02 - Objects - Lavrentyev FedorProgramming Java - Lecture 02 - Objects - Lavrentyev Fedor
Programming Java - Lecture 02 - Objects - Lavrentyev FedorFedor Lavrentyev
 
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorProgramming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorFedor Lavrentyev
 
Programming Java - Lection 05 - Software Design - Lavrentyev Fedor
Programming Java - Lection 05 - Software Design - Lavrentyev FedorProgramming Java - Lection 05 - Software Design - Lavrentyev Fedor
Programming Java - Lection 05 - Software Design - Lavrentyev FedorFedor Lavrentyev
 
Programming Java - Lection 06 - Multithreading - Lavrentyev Fedor
Programming Java - Lection 06 - Multithreading - Lavrentyev FedorProgramming Java - Lection 06 - Multithreading - Lavrentyev Fedor
Programming Java - Lection 06 - Multithreading - Lavrentyev FedorFedor Lavrentyev
 
Industrial Programming Java - Lection Pack 02 - Distributed applications - La...
Industrial Programming Java - Lection Pack 02 - Distributed applications - La...Industrial Programming Java - Lection Pack 02 - Distributed applications - La...
Industrial Programming Java - Lection Pack 02 - Distributed applications - La...Fedor Lavrentyev
 
Industrial Programming Java - Lection Pack 01 - Building an application - Lav...
Industrial Programming Java - Lection Pack 01 - Building an application - Lav...Industrial Programming Java - Lection Pack 01 - Building an application - Lav...
Industrial Programming Java - Lection Pack 01 - Building an application - Lav...Fedor Lavrentyev
 
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev FedorProgramming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev FedorFedor Lavrentyev
 
Industrial Programming Java - Lection Pack 03 - Relational Databases - Lavren...
Industrial Programming Java - Lection Pack 03 - Relational Databases - Lavren...Industrial Programming Java - Lection Pack 03 - Relational Databases - Lavren...
Industrial Programming Java - Lection Pack 03 - Relational Databases - Lavren...Fedor Lavrentyev
 
Top 15 Adventures Destinations You Should Try Out
 Top 15 Adventures Destinations You Should Try Out Top 15 Adventures Destinations You Should Try Out
Top 15 Adventures Destinations You Should Try OutTours In Abu Dhabi.Com
 
Practica 6 gerardo
Practica 6 gerardoPractica 6 gerardo
Practica 6 gerardogerardd98
 
Innovación educativa ana patricia revelo mejia y fabiola romero valenzuela y ...
Innovación educativa ana patricia revelo mejia y fabiola romero valenzuela y ...Innovación educativa ana patricia revelo mejia y fabiola romero valenzuela y ...
Innovación educativa ana patricia revelo mejia y fabiola romero valenzuela y ...Ana Patricia Revelo Mejía
 

Andere mochten auch (15)

Programming Java - Lection 01 - Basics - Lavrentyev Fedor
Programming Java - Lection 01 - Basics - Lavrentyev FedorProgramming Java - Lection 01 - Basics - Lavrentyev Fedor
Programming Java - Lection 01 - Basics - Lavrentyev Fedor
 
Programming Java - Lecture 02 - Objects - Lavrentyev Fedor
Programming Java - Lecture 02 - Objects - Lavrentyev FedorProgramming Java - Lecture 02 - Objects - Lavrentyev Fedor
Programming Java - Lecture 02 - Objects - Lavrentyev Fedor
 
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorProgramming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
 
Programming Java - Lection 05 - Software Design - Lavrentyev Fedor
Programming Java - Lection 05 - Software Design - Lavrentyev FedorProgramming Java - Lection 05 - Software Design - Lavrentyev Fedor
Programming Java - Lection 05 - Software Design - Lavrentyev Fedor
 
Programming Java - Lection 06 - Multithreading - Lavrentyev Fedor
Programming Java - Lection 06 - Multithreading - Lavrentyev FedorProgramming Java - Lection 06 - Multithreading - Lavrentyev Fedor
Programming Java - Lection 06 - Multithreading - Lavrentyev Fedor
 
Industrial Programming Java - Lection Pack 02 - Distributed applications - La...
Industrial Programming Java - Lection Pack 02 - Distributed applications - La...Industrial Programming Java - Lection Pack 02 - Distributed applications - La...
Industrial Programming Java - Lection Pack 02 - Distributed applications - La...
 
Industrial Programming Java - Lection Pack 01 - Building an application - Lav...
Industrial Programming Java - Lection Pack 01 - Building an application - Lav...Industrial Programming Java - Lection Pack 01 - Building an application - Lav...
Industrial Programming Java - Lection Pack 01 - Building an application - Lav...
 
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev FedorProgramming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
 
Industrial Programming Java - Lection Pack 03 - Relational Databases - Lavren...
Industrial Programming Java - Lection Pack 03 - Relational Databases - Lavren...Industrial Programming Java - Lection Pack 03 - Relational Databases - Lavren...
Industrial Programming Java - Lection Pack 03 - Relational Databases - Lavren...
 
Robot
RobotRobot
Robot
 
rocio roa
rocio roarocio roa
rocio roa
 
Top 15 Adventures Destinations You Should Try Out
 Top 15 Adventures Destinations You Should Try Out Top 15 Adventures Destinations You Should Try Out
Top 15 Adventures Destinations You Should Try Out
 
Practica 6 gerardo
Practica 6 gerardoPractica 6 gerardo
Practica 6 gerardo
 
Innovación educativa ana patricia revelo mejia y fabiola romero valenzuela y ...
Innovación educativa ana patricia revelo mejia y fabiola romero valenzuela y ...Innovación educativa ana patricia revelo mejia y fabiola romero valenzuela y ...
Innovación educativa ana patricia revelo mejia y fabiola romero valenzuela y ...
 
Estructura 7
Estructura 7Estructura 7
Estructura 7
 

Programming Java - Lection 03 - Classes - Lavrentyev Fedor

  • 1. 3. Классы Программирование на Java Федор Лаврентьев МФТИ, 2016
  • 3. Файл java/lang/Integer.java package java.lang; public class Integer { public static final int MAX_VALUE = ...; ... }
  • 4. Файл ru/mipt/java/lection3/Example.java package ru.mipt.java.lection3; [import java.lang.Integer;] import java.io.InputStream; import static java.lang.Integer.MAX_VALUE; import static java.lang.Integer.toHexString; public class Example { Integer maxInt = MAX_VALUE; String maxString = toHexString(MAX_VALUE); InputStream is = new java.io.FileInputStream(“foo.txt”); InputSteam is2 = new ru.mipt.java.lection3. FileInputStream(); }
  • 5. Файлы и директории ru/ - mipt/ - java/ |- lection3/ | |- Child.java | |- Example.java | - Parent.java - lection4/ - Utility.java java/ |- io/ | |- IOException.java | - InputStream.java - lang/ - String.java
  • 7. Наследование class Rectangle { final double width, height; Rectangle(double width, double height) { this.width = width; this.height = height; } } class Square extends Rectangle { Square(double side) { super(side, side); } }
  • 8. LSP (Barbara Liskov’s Substitution Principle) double calcArea(Rectangle r) { return r.getWidth() * r.getHeight(); } Rectangle a = new Rectangle(4, 5); calcArea(a); // 20 Rectangle b = new Square(4); calcArea(b); // 16 Square c = new Square(5); calcArea(c); // 25
  • 9. Приведение и проверка типов public void draw(Shape shape) { if (shape instanceof Rectangle) { Rectangle rect = (Rectangle) shape; drawRectangle(rect); } else if (shape instanceof Circle) { Circle circle = (Circle) shape; drawCircle(circle); } else if (shape instanceof Line) { drawLine((Line) shape); } }
  • 10. Абстрактные классы abstract class Shape { abstract double calcArea(); } class Rectangle extends Shape { ... @Override double calcArea() { return getWidth() * getHeight(); } }
  • 11. Переопределение метода class Object { public int hashCode() { ... } } class Long [extends Object] { private final long value; @Override public int hashCode() { return (int) (value ^ (value >>> 32)) } }
  • 12. Вызов метода родительского класса class Circle { public void draw() { Canvas.circle(x, y, radius); } } class Sun extends Circle { @Override public void draw() { super.draw(); // Draw circle drawSunlight(); } }
  • 13. Инстанцирование абстрактного класса Shape a = new Rectangle(3, 4); a.calcArea(); // 12 Shape b = new Square(7); b.calcArea(); // 49 Shape c = new Shape(); c.calcArea(); // ?
  • 14. Инстанцирование абстрактного класса Shape a = new Rectangle(3, 4); a.calcArea(); // 12 Shape b = new Square(7); b.calcArea(); // 49 Shape c = new Shape(); // WRONG! c.calcArea();
  • 15. Анонимные классы Shape a = new Rectangle(3, 4); a.calcArea(); // 12 Shape b = new Square(7); b.calcArea(); // 49 Shape c = new Shape() { double calcArea() { return 0; } } c.calcArea(); // 0
  • 17. Инкапсуляция - private и public public class Animal { private double x; private double y; private double bearing; public double getX() { return x; } public double getY() { return y; } public double getBearing() { return bearing; } public void rotate(double angle) { bearing = modulo(bearing + angle, 2 * Math.PI); } public void stepUp(double distance) { x += distance * Math.cos(bearing); y += distance * Math.sin(bearing); } }
  • 18. Точки расширения - protected public class Animal { ... private boolean hungry; public boolean isHungry() { return hungry;} protected void setHungry(boolean hungry) { this.hungry = hungry; } } public class LazyAnimal { @Override public void stepUp(double distance) { if (isHungry()) throw new IllegalStateException(); super.stepUp(distance); setHungry(true); } }
  • 19. Доступ по умолчанию (package-local) package edu.phystech.java.lection3; public class Animal { ... String name; } public class Dog extends Animal { public String tellName() { return “Bark!”; } } public class Student extends Animal { public String tellName() { return name; } } public class Zookeeper { public String nameAnimal(Animal a) { return a.name; } }
  • 20. Хорошая, годная инкапсуляция public class Wallet { private double money = 0.0; public void putMoney(double amount) { if (amount < 0) throw new IllegalArgumentException(); money += amount } public void takeMoney(double amount) { if (amount < 0) throw new IllegalArgumentException(); if (amount > money) throw new IllegalArgumentException(); money -= amount; } public double getMoney() { return money; } }
  • 21. Нарушение инкапсуляции public class Wallet { public double money = 0.0; public void putMoney(double amount) { if (amount < 0) throw new IllegalArgumentException(); money += amount } public void takeMoney(double amount) { if (amount < 0) throw new IllegalArgumentException(); if (amount > money) throw new IllegalArgumentException(); money -= amount; } public double getMoney() { return money; } }
  • 22. Public Morozoff в один ход public class Wallet { public double money; ... } public static void leakMoney(Wallet w) { w.money = -5; }
  • 23. Возможное нарушение инкапсуляции public class Wallet { protected double money = 0.0; public void putMoney(double amount) { if (amount < 0) throw new IllegalArgumentException(); money += amount } public void takeMoney(double amount) { if (amount < 0) throw new IllegalArgumentException(); if (amount > money) throw new IllegalArgumentException(); money -= amount; } public double getMoney() { return money; } }
  • 24. Public Morozoff в два хода public class Wallet { protected double money; ... } pubic class LeakingWallet extends Wallet { public void setMoney(double amount) { money = amount; } } public void leakMoney(LeakingWallet w) { w.setMoney(-5); }
  • 25. Внутренние классы public class Dog { private double bearing; // Направление public class Head { private final int length; public String smash() {} } }
  • 26. Вложенные классы public class Observable { public static class Event { private final Observable source; private final String type; private final String cause; } public static class Observer { void onEvent(Event e) { ... } } }
  • 28. Интерфейсы public interface Planar { [public] void setLocation(Location l); [public] Location getLocation(); } public class Rectangle implements Planar { @Override void setLocation(Location l) { this.left = l.getX(); this.top = l.getY(); } @Override void getLocation() { return new Location(getLeft(), getTop()); } }
  • 29. Абстрактная имплементация интерфейса public interface Planar { void setLocation(Location l); Location getLocation(); } public abstract class Shape implements Planar { // setLocation() unimplemented // getLocation() unimplemented } public class Rectangle extends Shape { ... }
  • 30. Наследование интерфейсов public interface Planar { void setLocation(Location l); Location getLocation(); } public interface Drawable extends Planar { void draw(Canvas c); } public class Shape implements Drawable { ... }
  • 31. Множественное наследование public class Geometry { ... } public class Circle extends Geometry implements Drawable, Comparable, Closeable { ... }
  • 32. Реализация по умолчанию public interface Observable { List<Observer> getObservers(); void addObserver(Observer o) default { getObservers().add(o); } Observer removeObserver(Observer o) default { getObservers().remove(o); } void notifyObservers(Event e) default { List<Observer> observers = getObservers(); for (int i = 0; i <= observers.size(); ++i) { observers.get(i).onEvent(e); } } }
  • 34. Сравнение по ссылке Student s1 = new Student(“Vasya”, 21); Student s2 = new Student(“Vasya”, 21); Student s3 = s1; assert s1 == s1; // true assert s1 == s2; // false assert s1 == s3; // true
  • 35. Сравнение по ссылке Integer a = 100500; Integer b = Integer.valueOf(100500); Integer c = Integer.valueOf(100500); assert a == b; assert a == c; assert b == c; Integer d = 42; Integer e = Integer.valueOf(42) assert d == e;
  • 36. Сравнение по ссылке Integer a = 100500; Integer b = Integer.valueOf(100500); Integer c = Integer.valueOf(100500); assert a == b; // WRONG assert a == c; // WRONG assert b == c; // WRONG Integer d = 42; Integer e = Integer.valueOf(42) assert d == e;
  • 37. Сравнение по ссылке Integer a = 100500; Integer b = Integer.valueOf(100500); Integer c = Integer.valueOf(100500); assert a == b; // WRONG assert a == c; // WRONG assert b == c; // WRONG Integer d = 42; Integer e = Integer.valueOf(42) assert d == e; // CORRECT?!
  • 38. Сравнение объектов – equals class Student { ... public boolean equals(Object o) { if (o == null) return false; if (this == o) return true; if (this.getClass() != o.getClass()) return false; Student s = (Student) o; if (this.age != s.age) return false; if (this.name == null) return s.name == null; return this.name.equals(s.name); } }
  • 39. Сравнение объектов – equals (в Java 7) class Student { ... public boolean equals(Object o) { if (o == null) return false; if (this == o) return true; if (this.getClass() != o.getClass()) return false; Student s = (Student) o; if (this.age != s.age) return false; return Objects.equals(this.name, s.name); } }
  • 40. Сравнение объектов – хеш-код class Student { ... public int hashCode() { final int prime = 37; return age * prime + name.hashCode(); } }
  • 41. Сравнение объектов – по умолчанию public class Object { ... public native int hashCode(); // адрес в памяти public boolean equals(Object o) { return this == o; } }
  • 42. Контракт на сравнение объектов • a.equals(a) == true; // Рефлексивность • a.equals(b) == b.equals(a); // Симметричность • a.equals(b) && b.equals(c) => a.equals(c); // Транзитивность • a.equals(b) == a.equals(b); //Консистентность • a.hashCode() == a.hashCode(); //Консистентность • a.equals(b) == true => a.hashCode() == b.hashCode(); • a.equals(null) == false;
  • 43. Enum
  • 44. Enum public enum TaskState { QUEUED, RUNNING, FINISHED, FAILED; } import static TaskState.*; public class Task { private TaskState state; void updateState(TaskState nextState) { if (state == FINISHED || state == FAILED) { throw new IllegalStateException(); } if (state == RUNNING && nextState == QUEUED) { throw new IllegalStateException(); } state = nextState; } }
  • 45. Расширение enum public enum TaskState { QUEUED(0), RUNNING(1), FINISHED(2), FAILED(2); private final int order; private TaskState(int order) { this.order = order; } public boolean isUpdateAllowed(TaskState next) { return next.order > this.order; } }
  • 46. Расширение enum import static TaskState.*; public class Task { private TaskState state; void updateState(TaskState nextState) { if (!state.isUpdateAllowed(nextState) { throw new IllegalStateException(); } state = nextState; } }

Hinweis der Redaktion

  1. Пэкейдж java закрыт для расширения
  2. Rectangle implements Planar
  3. Rectangle implements Drawable
  4. Enum – аналог запечатанного списка констант данного типа