【SC随笔】Java预备知识

【自用】CS61b的一些笔记

Instance Variables and Object Instantiation

  • An Object in Java is an instance of any class.
  • The Dog class has its own variables, also known as instance variables or non-static variables. Unlike languages like Python or Matlab, these must be declared inside the class, where new variables can be added at runtime.
  • The method that we created in the Dog class did not have the static keyword. We call such methods instance methods or non-static methods.
  • To call the makeNoise method, we had to first instantiate a Dog using the new keyword, and then make a specific Dog bark. In other words, we called d.makeNoise() instead of Dog.makeNoise().
  • Once an object has been instantiated, it can be assigned to a declared variable of the appropriate type, e.g. d = new Dog();
  • Variables and methods of a class are also called members of a class.
  • Members of a class are accessed using dot notation.
public class Dog {
    public int weightInPounds;

    public void makeNoise() {
        if (weightInPounds < 10) {
            System.out.println("yipyipyip!");
        } else if (weightInPounds < 30) {
            System.out.println("bark. bark.");
        } else {
            System.out.println("woof!");
        }
    }    
}

Constructors in Java

When the instantiation is parameterized, saving us the time and messiness of manually typing out potentially many instance variable assignments

public class Dog {
    public int weightInPounds;

    public Dog(int w) {
        weightInPounds = w;
    }

    public void makeNoise() {
        if (weightInPounds < 10) {
            System.out.println("yipyipyip!");
        } else if (weightInPounds < 30) {
            System.out.println("bark. bark.");
        } else {
            System.out.println("woof!");
        }    
    }
}
public class DogLauncher {
    public static void main(String[] args) {
        Dog d = new Dog(20);
        d.makeNoise();
    }
}

Class Methods vs. Instance Methods

Java allows us to define two types of methods:

  • Class methods, a.k.a. static methods.
  • Instance methods, a.k.a. non-static methods.

Instance methods are actions that can be taken only by a specific instance of a class.

Static methods are actions that are taken by the class itself.

Both are useful in different circumstances. As an example of a static method, the Math class provides an sqrt method. Because it is static, we can call it as follows:

x = Math.sqrt(100);

If sqrt had been an instance method, we would have instead of the awkward syntax below. Luckily sqrt is a static method so we don’t have to do this in real programs.

Math m = new Math();
x = m.sqrt(100);

Static Variables

It is occasionally useful for classes to have static variables. These are properties inherent to the class itself, rather than the instance. For example, we might record that the scientific name (or binomen) for Dogs is “Canis familiaris”:

public class Dog {
    public int weightInPounds;
    public static String binomen = "Canis familiaris";
    ...
}

Static variables should be accessed using the name of the class rather than a specific instance, e.g. you should use Dog.binomen, not d.binomen.

While Java technology allows you to access a static variable using an instance name, it is bad style, confusing, and in my opinion an error by the Java designers.

primitive types vs reference type

  • 8 primitive types in java: byte short int long float double boolean char, everything else, including arrays, is a reference type.
    • primitive typesa = b给a开辟了新memory,改变a不改变b;而reference typea = b是另指针a指向b的内存,改变了a等于改变了b
    • primitive types传递给函数形参,reference type传递给函数指针

-

Terminology Summary

int i=Integer.parseInt("200");  //类型转化

String[] a = {"cat", "dog", "laser horse", "ketchup"}; for (String s : a) {...}      //间接版for循环

A.equals(B);//比较reference type 是否相同

  • 对于每次通过主循环,update在计算所有力并安全存储在xForces 和之前不要调用 yForces。例如,planets[0].update()在整个xForcesyForces数组完成之前不要调用!差异很微妙,但如果您planets[0].update在计算xForces[1]和之前调用自动分级器会感到不安yForces[1]

  • p+tap = System.out.print();

-

  • 类 可以嵌套 类 (设内部的类为B,外部的类为A),如果B不被除了A之外成员访问,可以将B设为private,如果B不访问A中的成员,可以将B设为static。一旦B被设为private,B中成员的修饰词(public or private)就无关紧要了。

  • 例1传递给参数pokemon的指针,可以改变指针所指的内容poke.level,但不会改变指针所指poke = new pokemon()

  • 例1深刻理解static variable;例2Horse same这个变量只在if里有效,出了if的大括号所有的same都是类的成员same,而不是一个名叫same的Horse的实例;例4中虽然数组传递的是指针,但for(int x : A)x也只是在for中有效的形参,不能改变数组的值,用for(int i = 0; i<A.length; i++)就能改变数组的值

- <img src="C:\Users\ljy\AppData\Roaming\Typora\typora-user-images\image-20220127161604829.png" alt="image-20220127161604829" style="zoom:50%;" /
>

  • overloaded and have the same signature is overridden

    在这里插入图片描述

  • [exercise](*examprep04sol.pdf (datastructur.es))

  • wrapper types and primitives can be used almost interchangeably. But Arrays are never autoboxed/unboxed, e.g. and Integer[] can not be used in place of an int[] (or vice versa).

  • interface vs abstract class: Unlike interfaces, abstract classes can provide implementation inheritance for features other than public methods, including instance variables. In interface, methods are abstract(without braces, and followed by a semicolon), unless it has keywords default. However, in abstract class, abstract methods should be followed by keywords abstract.

    • think of an interface as defining a “can-do” or an “is-a” relationship, whereas an abstract class should be a stricter “is-a” relationship. The difference can be subtle, and can often use one instead of the other.

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
评论

打赏作者

ClarifiedfishLEE

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值