public class Point {
/* Fields are defined here */
}
class Point {
/* Fields */
/** Point x coordinate */
private int x;
/** Point y coordinate */
private int y;
}
class Point {
/* Fields */
/** Point x coordinate */
private int x;
/** Point y coordinate */
private int y;
/* Methods */
/** Return the x coordinate */
public int getX() { return x; }
/** Return the y coordinate */
public int getY() { return y; }
/** Set the point position to the specified coordinates */
public void setPosition(int sx, int sy) {
x = sx;
y = sy;
}
/** Return string representation */
@Override public String toString() {
return "x=" + Integer.valueOf(x).toString() +
" y=" + Integer.valueOf(y).toString();
}
}
this
. class Point {
/** Set the point position to the specified coordinates */
public void setPosition(int sx, int sy) {
x = sx;
y = sy;
}
}
class Point {
/** Set the point position to the specified coordinates */
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
}
class Point {
/* Fields */
/** Point x coordinate */
private int x;
/** Point y coordinate */
private int y;
/* Methods */
/** Default constructor */
Point() { x = y = 0; }
/** Constructor with coordinates */
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
class Point {
private int x, y;
/** Count number of points used */
private static int numPoints = 0;
/** Point constructor */
Point(int ix, int iy) {
x = ix;
y = iy;
numPoints++; // Adjust points counter
}
/** Point constructor */
Point() {
x = y = 0;
numPoints++; // Adjust points counter
}
// Return number of points currently used
public static int getNumPoints() {
return numPoints;
}
static public void main(String args[]) {
Point a = new Point(1, 2);
Point b = new Point(5, 8);
Point c = b;
System.out.println(getNumPoints() + " points have been created");
}
}
{ }
στο σώμα της κλάσης μπορεί να τοποθετηθεί:
class Rectangle {
private static class Point {
private int x, y;
/** Point constructor */
Point(int ix, int iy) {
x = ix;
y = iy;
}
}
private Point topLeft, bottomRight;
/** Rectangle constructor */
Rectangle(int x, int y, int height, int width) {
topLeft = new Point(x, y);
bottomRight = new Point(x + width, y + height);
}
}
class InnerCall {
public static void main(String args[]) {
System.out.println(new Object());
System.out.println(new Object() {
@Override
public String toString() {
return "I am a woke object";
}
});
}
}
enum Ingredients {
TOMATO,
ONION,
TZATZIKI,
POTATO,
MUSTARD,
SOUVLAKI,
GYROS
};
public final
μέλος της κλάσης.case
,
π.χ. case Ingredients.POTATO:
.int ordinal()
επιστρέφει τη θέση ενός στοιχείου στην απαρίθμηση.String name()
επιστρέφει το όνομα ενός στοιχείου.valueOf
επιστρέφει την τιμή της απαρίθμησης με βάση συμβολοσειρά με το όνομά της, π.χ. Enum.valueOf(Ingredients.class, "TOMATO")
.values
επιστρέφει έναν πίνακα με τις τιμές μιας απαρίθμησης.
public class Ingredients {
// Static array to hold all ingredient constants
private static final Ingredients[] VALUES = new Ingredients[7];
// Constants with names and ordinal values
public static final Ingredients TOMATO = new Ingredients("TOMATO", 0);
public static final Ingredients ONION = new Ingredients("ONION", 1);
public static final Ingredients TZATZIKI = new Ingredients("TZATZIKI", 2);
public static final Ingredients POTATO = new Ingredients("POTATO", 3);
public static final Ingredients MUSTARD = new Ingredients("MUSTARD", 4);
public static final Ingredients SOUVLAKI = new Ingredients("SOUVLAKI", 5);
public static final Ingredients GYROS = new Ingredients("GYROS", 6);
private final String name;
private final int ordinal;
private Ingredients(String name, int ordinal) {
this.name = name;
this.ordinal = ordinal;
VALUES[ordinal] = this; // Add to the static array
}
// Getters
public int ordinal() {
return ordinal;
}
public String getName() {
return name;
}
public static Ingredients[] values() {
return VALUES.clone(); // Return a copy of the array
}
public static Ingredients valueOf(String name) {
for (Ingredients ingredient : VALUES) {
if (ingredient.name.equals(name)) {
return ingredient;
}
}
throw new IllegalArgumentException("No enum constant " + name);
}
@Override
public String toString() {
return name;
}
}
switch
μπορούμε να
γράψουμε κώδικα για συγκεκριμένους τύπους με
βάση το ταίριασμα προτύπων (pattern matching).
class PaternMatch {
class RetailCustomer {}
class BusinessCustomer {}
/** Return the document a given object shall receive */
private static String documentName(Object o) {
return switch (o) {
case RetailCustomer rc -> "receipt";
case BusinessCustomer bc -> "invoice";
default -> "unknown document";
};
}
public static void main(String[] args) {
var retailCustomer = new RetailCustomer();
var businessCustomer = new BusinessCustomer();
System.out.println("Retail customer gets "
+ documentName( retailCustomer));
System.out.println("Business customer gets "
+ documentName(businessCustomer));
}
}
public class RecordDemo {
record Point(int x, int y) {}
public static void main(String[] args) {
var a = new Point(5, 12); // Pythagorean triple
System.out.println("Point " + a + " has magnitude "
+ Math.sqrt(a.x() * a.x() + a.y() * a.y()));
}
}
public class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
// Getters
public int getX() {
return x;
}
public int getY() {
return y;
}
// Object methods
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return x == point.x && y == point.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public String toString() {
return "Point{" + "x=" + x + ", y=" + y + '}';
}
}
class PaternMatch {
record Point2D(double x, double y) {}
record Point3D(double x, double y, double z) {}
/** Output the magnitude of the specified object */
private static void printMagnitude(Object o) {
System.out.println("Magnitude of " + o + " is " +
switch (o) {
case Point2D(double x, double y) -> Math.sqrt(x * x + y * y);
case Point3D(double x, double y, double z) ->
Math.sqrt(x * x + y * y + z * z);
default -> throw new IllegalArgumentException("Unexpected type: "
+ o.getClass().getName());
}
);
}
public static void main(String[] args) {
printMagnitude(new Point2D(3, 4)); // Pythagorean triple
printMagnitude(new Point3D(1, 4, 8)); // Pythagorean quadruple
}
}
public class Accounts {
/** Single instance of a company's accounts. */
private static final Accounts instance = new Accounts();
/** Return the single instance of the company's accounts. */
public static Accounts getInstance() {
return instance;
}
/** Supress public default constructor to assure non-instantiation */
private final Accounts() {
// Never invoked from outside the class
}
}