Declarations and Access Control (2)

2016-02-19 13:30 2 1 收藏

图老师小编精心整理的Declarations and Access Control (2)希望大家喜欢,觉得好的亲们记得收藏起来哦!您的支持就是小编更新的动力~

【 tulaoshi.com - 编程语言 】

Civilization
·igfxpers - igfxpers.exe - Process In
  Objective 2
Declare classes, inner classes, methods, instance variables static, variables and automatic (method local) variables, making appropriate use of all permitted modifiers (such as public final static abstract and so forth). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.
1. Two types of variables.
1.    Member variables
·    Accessible anywhere in the class.
·    Automatically initialized before invoking any constructor.
·    Static variables are initialized at class load time.
·    Can have the same name as the class.
2.    Automatic variables(method local)
·    Must be initialized explicitly. (compiler will catch it when using, but doesn’t catch it if no using) Object references can be initialized to null to make the compiler happy.
·    Can have the same name as a member variable, resolution is based on scope.
·    Can only be final.  Not other modifiers.

2.   Modifiers are Java keywords that provide information to compiler about the nature of the code, data and classes.   The visibility modifiers are part of the encapsulation mechanism for Java. Encapsulation allows separation of the interface from the implementation of methods.


3.  Access modifiers – public, protected, private
·    Only applied to class level variables. (Method variables are visible only inside the method.)
·    Can be applied to class itself (only to inner classes declared at class level, no such thing as protected or private top level class)
·    Can be applied to methods and constructors.
·    If a class is accessible, it doesn’t mean, the members are also accessible. But if the class is not accessible, the members are not accessible, even though they are declared public.
·    If no access modifier is specified, then the accessibility is default package visibility. All classes in the same package can access the feature. It’s called as friendly access. But friendly is not a Java keyword. Same directory is same package in Java’s consideration.
·    Only one outer class per file can be declared public.  If you declare more than one class in a file to be public, a compile time error will occur.
·    ‘private’ means only the class can access it, not even sub-classes.  So, it’ll cause access denial to a sub-class’s own variable/method.
·    These modifiers dictate, which classes can access the features. An instance of a class can access the private features of another instance of the same class.
·    ‘protected’ means all classes in the same package (like default) and sub-classes in any package can access the features. But a subclass in another package can access the protected members in the super-class via only the references of subclass or its subclasses. A subclass in the same package doesn’t have this restriction. This ensures that classes from other packages are accessing only the members that are part of their inheritance hierarchy.
·    Methods cannot be overridden to be more private. Only the direction shown in following figure is permitted from parent classes to sub-classes.

private à friendly (default) à protected à public

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

    Parent classes                     Sub-classes

4.  final
·    final classes cannot be sub-classed.
·    final variables cannot be changed.
·    final methods cannot be overridden.  Any methods in a final class are automatically final.   
·    Method arguments marked final are read-only. Compiler error, if trying to assign values to final arguments inside the method.
·    Final variables that are not assigned a value at the declaration and method arguments that are marked final are called blank final variables. Try to use blank final variables will give compile error.  They can only be assigned a value at most once in all constructor or initialized block.
·    Static final variables have to be assigned at the declaration time or in static initialized block.
·    Local variables can be declared final as well.

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

5.  abstract
·    Can be applied to classes and methods.
·    Opposite of final, abstract must be sub-classed.
·    A class should be declared abstract,
1.    if it has any abstract methods.
2.    if it doesn’t provide implementation to any of the abstract methods it inherited
3.    if it doesn’t provide implementation to any of the methods in an interface that it says implementing.
·    Just terminate the abstract method signature with a ‘;’, curly braces will give a compiler error.
·    A class can be abstract even if it doesn’t have any abstract methods.
·    Abstract methods may not be static, final, private, native, synchonized.
·    A class that is abstract may not be instantiated (ie, you may not call its constructor, but in subclass’s constructor, super() works)

6.  static
·    Can be applied to nested classes, methods, variables, free floating code-block (static initializer)
·    static means one per class, not one for each object no matter how many instance of a class might
exist. This means that you can use them without creating an instance of a class.
·    Static variables are initialized at class load time. A class has only one copy of these variables.
·    Static methods can access only static variables. (They have no this)
·    Access by class name is a recommended way to access static methods/variables.
·    Static methods may not be overridden to be non-static.
·    Non-static methods may not be overridden to be static.
·    Local variables cannot be declared as static.
·    Actually, static methods are not participating in the usual overriding mechanism of invoking the methods based on the class of the object at runtime. Static method binding is done at compile time, so the method to be invoked is determined by the type of reference variable rather than the actual type of the object it holds at runtime.

public class StaticOverridingTest {
  public static void main(String s[]) {
    Child c = new Child();
    c.doStuff(); // This will invoke Child.doStuff()
    Parent p = new Parent();
    p.doStuff(); // This will invoke Parent.doStuff()
    p = c;
    p.doStuff(); // This will invok

来源:https://www.tulaoshi.com/n/20160219/1604483.html

延伸阅读
Microsoft Access是用户经常使用的数据库软件,它可以用ODBC很方便地得到AS/400上DB2数据。 具体操作如下:(操作步骤用WIN2000+Office2000环境描述,用户已配置好ODBC数据源)。 1、打开Microsoft Access,建立一个新数据库。 2、在“表”上按鼠标右键,之后用鼠标左键单击“导入”菜单项,如图1所示。之后弹出“导入”...
标签: 电脑入门
故障现象: motion control软件开启后,图标是橙色的,但是为何不能使用手势功能? 解决方案: 1. 软件在启用的状态下为橘黄色手图标,关闭的状态下是小手图标是灰色的(右键点击小手图标可以选择启用)。 2. 如果用户表示软件已经在启用状态依然无法使用手势,原因是此手势功能只支持部分软件,所以请确认用户目前使用的软件。 支持的播...
标签: Web开发
1. Accordion Accordion可以让你设计多个panel 并且一次只显示一个Panel .在页面上的显示效果就像是使用了多个CollapsiblePanels只不过每一次只展开其中一个CollapsiblePanel.Accordion控件内部包含了若干个AccordionPane,每一个AccordionPane的template里包括了对其Header和Content的定义。我们可以在后台代码中通过SelectedIndex属性取得...
标签: ASP
  1、如果需要对数据库进行写和更新操作,有时候设置数据库的写权限! 2、对数据库进行更新操作时,最好锁定数据库,以免有其他人同时进行此操作时,发生冲突。 3、为安全起见,应该为数据库设置密码访问,虽然破解比较简单,但是有一层安全权限当然比没有要好一些。 4、为数据库另外设置目录,并为数据起一个特别的名字,尽量是不常见的...
在近日的写Web程序时用到了Access的模糊查询,在Acces里写代码怎么也找不到记录,后来才起来原来Acess和SqlServer的模糊查询是有特别的 条件:查找表A 的Name字段中包括 "B" 的记当 在Access里的代码: 1 Select * from a where name like '*b*'Sql Server查询分析器的代码 Select * from a where name like '%b%'这时你会发现Acc...

经验教程

947

收藏

26
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部