Java 的介绍 (2)

在之前,小编已经总结了一些Java的语言跟C++语言的不同,现在继续介绍Java, 同样还是采用大部分的比例都使用英文来描述。如果有什么遗漏的部分,欢迎大家的指正。

  1. Everything we do in Java is part of a class. This means that none of our functions can be “global” like they can be in C or C++.
  2. Classes in Java specify types, as they do in C++ and allow use to create abstractions.
    [1] Classes must be specified as public, or not.
    [2] Only public classes are available for the outside world
    [3] If the keyword public doesn’t precede a class, then it is “friendly” and only classes from within this file or package (a group of files) can create objects. Every package (or file to begin with) has a public class.

  3. Inside of a class, Java supports public, protected, and private access (or nothing - which means “friendly” access).
    [1] But unlike C++, it requires that they be specified in front of each member rather than specifying categories!
    [2] This means that everything has some kind of access specified for it.

Class Access Visibility
Unlike C++, a class can be specified as public, or not. A public class within a library specifies which class(es) are available to a client programmer.
1. The public keyword just has to be placed somewhere before the opening brace ({) of the class body.
举个例子:
public class Example {

}
2. There can be only one public class per compilation unit
3. They must be the same name as the file
举个例子:
public class Example{

}
如果这个class 的名字 “Example” 跟 file 的名字不同,那么编译器就不会找到相对应的class,那么这样就无法运行这个程序
4. Without the public qualifier, a class becomes “friendly”, available to the other classes in the library to which it belongs.
5. Classes cannot be private or protected.
6. If you don’t want anyone access to a class, then make the constructors private!

Members of a Class
1. Like C++, classes in Java can have memebr functions (called methods) and data members.
[1] These look the same as in C++ except for the access control specifier
[2] The implementation of the member functions is provided (in most cases) in the class itseld - not elsewhere.

  1. Each member can be specified as public, private, or protected.

  2. If a member has no access control (public, private, or protected), they are treated as “friendly
    [1] The default means “friendly”: all other classes in the current package have access but all classes outside of this package are private.
    [2] This allows us to make data members and member functions semi “gloabl” - the scope is somewhat broader than C++ static global but restricted from actually be global in nature.
    [3] It allows us to create a library (a package) and allow classes to access members directly.
    [4] This helps us to organize classes together in a meaningful way.

Class Access Specifiers
1. What is the meaning in Java of public, private, and protected?
2. As is expected, public means that the member following is public to everyone using this library.
3. Private is not available - only other methods within the class can access that member. (Helper methods should be private)
4. Protected members are available within this class and a derived class (same as C++)
5. Recommendation: limit your use of the default friendly access. (Please note, a derived class may not be able to access its parent’s “friendly” members if they are not in the same package! This is because it is possible to inherit from a class that is NOT in the same package.)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值