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 the class 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 always done 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.

posted @   M1stF0rest  阅读(146)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示