Core Java Basics

这篇博客介绍了Java中接口的概念,通过Displaceable接口展示了如何定义和实现接口。同时,讲解了多态性的概念,如何使用接口实现多态方法调用。文中还涉及了静态与动态方法的区别,并给出了数组操作的示例,包括单维和多维数组的创建及遍历。此外,讨论了方法签名和结构相等的重要性。
摘要由CSDN通过智能技术生成

Java Interface

public interface Displaceable {
  public int getX ();
  public int getY ();
  public void move(int dx, int dy);
}

public class Point implements Displaceable {
  private int x, y;
  public Point(int x0, int y0) {
    x = x0;
    y = y0;
  }
  public int getX() { return x; }
  public int getY() { return y; }
  public void move(int dx, int dy) {
    x = x + dx;
    y = y + dy;
  }
}

class ColorPoint implements Displaceable {
  private Point p;
  private Color c;
  ColorPoint (int x0, int y0, Color c0) {
    p = new Point(x0,y0);
    c = c0;
  }
  public void move(int dx, int dy) {
    p.move(dx, dy);
  }
  public int getX() { return p.getX(); }
  public int getY() { return p.getY(); }
  public Color getColor() { return c; }
}

// This methods will work on an object created from any class with the interface
public void moveItALot(Displaceable s) {
  s.move(3,3);
  s.move(100, 1000);
  s.move(s.getX(), s.getY());
}

Core Java

int // standard integers
byte, short, long // other flavors of integers
char // unicode characters
float, double // floating-point numbers
boolean
image-20201030221929990
== 					// reference equality
a.equals(b) // structural equality

Static vs. Dynamic Methods

a dynamic property of the program—itsbehavior can’t be determined until the program is actually running

As the use of the keyword static implies, which code is called when a static method is invoked can be determined at compile time (without running the program). The way this works is that, rather than invoking a static method from an object, static methods are called relative to the class in which they are defined.

When should static methods be used? Generally they should be used for implementing functions that don’t depend on any objects’ states

Array

int [] arr = new Counter[10]
int[] myArray = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
String[] yourArray = { "foo", "bar", "baz" };
Point[] herArray = { new Point(1,3), new Point(5,4) };  

Because array elements are mutable, all of the issues with aliasing (recall §14) arise here as well.

public static double sum(double[] arr) {
  double total = 0;
  for (int i = 0; i < arr.length; i++) {
  	total = total + arr[i];
	}	
}

public static double sum(double[] arr) {
  double total = 0;
  int i = 0; // loop index initialization
  while (i < arr.length) { // loop guard
    total = total + arr[i];
    i++; // loop index update
  }
}

Multi-dimensional array isn’t neccessary to be a rectangle

String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
										{"Smith", "Jones"}};
for (int r=0; r < arr.length; r++){
  // use the length of the inner array
  for (int c=0; c < arr[r].length; c++) {
  	arr[r][c] = ...
	}
}

int[][] products = new int[5][];
for(int row = 0; row < 5; row++) {
  products[row] = new int[row+1];
  for(int col = 0; col <= row; col++) {
  	products[row][col] = row * col;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值