java中关于类_JAVA中关于类的几个性质

Client Programs and Main Methods. A Java program without a main method cannot be run directly using the java command. However, its methods can still be invoked using the main method of another class,and theclass that uses another class is sometimes called a "client" of that class

Class Declaration. Java classes can contain methods and/or variables. We say that such methods and variables are “members” of the class. Members can be instance members or static members. Static members are declared with the static keyword. Instance members are any members without the static keyword.

Class Instantiation. Instantiating a class is almost alwaysdone using the new keyword, e.g. Dog d = new Dog(). An instance of a class in Java is also called an “Object”.

Dot Notation. We access members of a class using dot notation, e.g. d.bark(). Class members can be accessed from within the same class or from other classes.

Constructors. Constructors tell Java what to do when a program tries to create an instance of a class, e.g. what it should do when it executes Dog d = new Dog().

The constructor with signature public Dog(int w) will be invoked anytime that we try to create a Dog using the new keyword and a single integer parameter. For those of you   coming from Python, the constructor is very similar to the __init__ method.

Array Instantiation. Arrays are also instantiated using the new keyword. For example int[] arr = new int[10] If we have an array of Objects, e.g. Dog[] dogarray, then each element of the array must also be instantiated separately.

Static vs. Instance methods. The distinction between static and instance methods is incredibly important. Instance methods are actions that can only be taken by an instance of the class (i.e. a specific object), whereas static methods are taken by the class itself. An instance method is invoked using a reference to a specific instance, e.g. d.bark(), whereas static methods should be invoked using the class name, e.g. Math.sqrt(). Know when to use each.

Static variables. Variables can also be static. Static variables should be accessed using the class name, e.g. Dog.binomen as opposed to d.binomen. Technically Java allows you to access using a specific instance, but we strongly encourage you not to do this to avoid confusion.

void methods. A method which does not return anything should be given a void return type.

The this keyword. Inside a method, we can use the this keyword to refer to the current instance. This is equivalent to self in Python.

public static void main(String[] args). We now know what each of these things means:

public: So far, all of our methods start with this keyword.

static: It is a static method, not associated with any particular instance.

void: It has no return type.

main: This is the name of the method.

String[] args: This is a parameter that is passed to the main method.

Command Line Arguments. Arguments can be provided by the operating system to your program as “command line arguments,” and can be accessed using the args parameter in main. For example if we call our program from the command line like this java ArgsDemo these are command line arguments, then the main method of ArgsDemo will have an array containing the Strings “these”, “are”, “command”, “line”, and “arguments”.

Using Libraries. There’s no need in the modern world to build everything yourself from scratch. In our course, you are allowed to and highly encouraged to use Java’s built-in libraries, as well as libraries that we provide, e.g. the Princeton standard library. You should not use libraries other than those provided or built into Java because it may render some of the assignments moot, and also our autograder won’t have access to these libraries and your code won’t work.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值