课后1-3练习笔记链接点击此处
教材是Programming Abstractions in Java, Eric.S.Roberts——Java程序设计 基础、编程抽象与算法策略。
你的三连就是我创作的最大动力!
两节笔记
1.类构成了层次结构,其中子类会继承超类的行为。在Java中,每个类都只有一个唯一的超类。
2.在Java中,不接受任何引元的构造器称为类的缺省构造器(自己编写的时候,注意按照如下的格式),会自动创建。
public class GPoint{
public GPoint(int x,int y){
this.x=x;
this.y=y;
}
}
3.this关键词,表示的是当前对象的引用,可以避免具有相同名字的参数变量混为一谈。
4.注意,每一个Java对象都一个toString()方法,定义新类时需要定义其实现。所以,要想明确用新定义来替代现有的方法定义是有意为之,惯例是在定义前编写@override来指明正在提供某个已有方法的新实现。
@override
public String toString(){}
对于override注解,编译器会执行检查确保已经覆盖超类的方法。
5.以 /** 注释,称为javadoc注释。常用的就是@param(产生有关方法参数的注释)和@return(描述方法的结果).
6.访问器(获取器)getName(); 修改器(设置器)setName()
7.Java中不允许我们对标准的操作符赋予新的含义,所以需要自己定义方法来实现其操作。(区别于C++操作符重载)
下面以一个Rational类来进行演示有理数的运算。
如果返回一个新类,注意return new classname(),当然也要注意到数据的私有性。
/**
* This class represents a rational number (the quotient of two integers).
*/
public class Rational {
/**
* Creates a new Rational initialized to zero.
*/
public Rational() {
this(0);
}
/**
* Creates a new Rational from the integer argument.
*
* @param n The initial value
*/
public Rational(int n) {
this(n, 1);
}
/**
* Creates a new Rational with the value x / y.
*
* @param x The numerator of the rational number
* @param y The denominator of the rational number
*/
public Rational(int x, int y) {
if (y == 0) throw new RuntimeException("Division by zero");
if (x == 0) {
num = 0;
den = 1;
} else {
int g = gcd(Math.abs(x), Math.abs(y));
num = x / g;
den = Math.abs(y) / g;
if (y < 0) num = -num;
}
}
/**
* Adds the current number (r1) to the rational number r2 and returns the sum.
*
* @param r2 The rational number to be added
* @return The sum of the current number and r2
*/
public Rational add(Rational r2) {
return new Rational(this.num * r2.den + r2.num * this.den,
this.den * r2.den);
}
/**
* Subtracts the rational number r2 from this one (r1).
*
* @param r2 The rational number to be subtracted
* @return The result of subtracting r2 from the current number
*/
public Rational subtract(Rational r2) {
return new Rational(this.num * r2.den - r2.num * this.den,
this.den * r2.den);
}
/**
* Multiplies this number (r1) by the rational number r2.
*
* @param r2 The rational number used as a multiplier
* @return The result of multiplying the current number by r2
*/
public Rational multiply(Rational r2) {
return new Rational(this.num * r2.num, this.den * r2.den);
}
/**
* Divides this number (r1) by the rational number r2.
*
* @param r2 The rational number used as a divisor
* @return The result of dividing the current number by r2
*/
public Rational divide(Rational r2) {
return new Rational(this.num * r2.den, this.den * r2.num);
}
/**
* Creates a string representation of this rational number.
*
* @return The string representation of this rational number
*/
@Override
public String toString() {
if (den == 1) {
return "" + num;
} else {
return num + "/" + den;
}
}
/**
* Calculates the greatest common divisor using Euclid's algorithm.
*
* @param x First integer
* @param y Second integer
* @return The greatest common divisor of x and y
*/
private int gcd(int x, int y) {
int r = x % y;
while (r != 0) {
x = y;
y = r;
r = x % y;
}
return y;
}
/* Private instance variables */
private int num; /* The numerator of this Rational */
private int den; /* The denominator of this Rational */
}
8.链接结构:就是链表了
对于链表一般定义如下:
public static class Tower{
String name;
Tower link;
}
下面以一个实例来进行说明。
public class Name{
public void run(){
Tower rohan=creat("Rohan", null);
Tower hali=creat("hali", rohan);
Tower amon=creat("amon", hali);
signal(amon);
}
private Tower creat(String name,Tower link){
Tower t=new Tower();
t.name=name;
t.link=link;
return t;
}
private void signal(Tower start){
for(Tower cp=start;cp!=null;cp=cp.link){
System.out.println(cp.name);
}
}
private static class Tower{
String name;
Tower link;
}
public static void main(String[] args){
new Name().run();
}
}
9.链表迭代,和C++类似
for(Class cp=start;cp!=null;cp=cp.link)
10.在Java中,定义子类的最基本形式如下:
class 子类 extends 超类{
子类中的新项
}
子类继承了超类中所有的公共类,而超类中私有部分仍保持私有。因此,子类不能直接访问其超类中的私有方法和实例变量。
public class integerList extends ArrayList<Integer>{
public integerList(int...args){
for(int k:args){
add(k);
}
}
public String toString(){
String s="";
for(int k:this){
if(!(s.isEmpty())) s+=",";
s+=k;
}
return "["+str+"]";
}
}
11.在Java中,在继承层次结构中最靠近该对象实际所属类的方法决定是继承还是覆盖。下面思考如下的问题:
IntegerList primes=new IntegerList(2,3,5,7);
Object obj=primes;
System.out.println(obj);
首先,将primes赋给obj是合理的,因为每个Java类都是Object的子类,但问题是Java处理println时,toString()方法的哪个版本会被调用?注意,存储在obj的值仍旧是一个IntegerList,所以会调用这个类的toString()。
12.在java中,可以使用super来调用超类的行为。
例如:
public Employee(String name){
super(name);//调用超类
}
13.访问权限:
总结
至此还差Java图形类还没有系统讲了,这里准备和C# WinForm还有C++ Qt做个比较汇总,总之,前端的东西也还在学习中,不过方法也都是相通的。然而后面还要大量练习Java知识,并通过数据结构和算法相应的知识来进行总结,当然学习Java的主要目的也是为了项目做好准备,争取也能学习一些技术,为日后的学习打下基础。