Java学习 [1]

0.Java四部分, package, import, public class, (default) class
1.构造方法只能在java在创建对象的时候默认的调用。普通方法内是不能调用构造方法,但构造方法内可以调用普通的方法。
2.This 用于访问实体变量或者调用方法的object的实体变量。this(实参)用此来调用同名构造方法,此this(实参)必须放在没有参数的构造方法的第一行。
同类名的构造方法,就是跟类的名字相同的method。在类创建后,编译器会偷偷帮你创建一个空同类名构造方法(属性一切为空)。

  1. 重载, 构造方法,多态包括 重载和覆盖

  2. 子类 extend 父类, 子类 可以通过new创建object 的引用,引用子类的属性或者方法也可以是父类的属性和方法,构造方法除外。子类 extend 父类, 子类 可以通过new创建object 的引用,引用子类的属性或者方法也可以是父类的属性和方法,构造方法除外。
    4.子类引用的变脸,通过给父类引用复制,而让父类引用看起来是指向子类的object,其实不是。 此时如果想彻底让父类引用彻底指向子类的object,需要让父类的引用做一个强制类型转换,可以借助Object 类来完成,因为Object是所有类的父类。只有子类与父类的继承有这种关系。

  3. package 用于帮助编译器定位源文件达到编译目的

  4. import引入类,用于帮助不同package下的累的相互引用。import 不能引用相同名字的类名,但是可以通过*替换其中一个类名,另外一个直接用import引用

  5. java 类或者变量的访问修饰,default, public,protected, private

  6. 方法或者变量修饰 final, 方法或者类不可以继承,变量不可以更改

  7. 静态方法, static,只能通过类名直接调用,不用通过引用。变量只能是静态方法内部局部变量,不可以是外部非静态变量。不可以使用super 和this。静态方法不能调用非静态方法或者外部的非静态变量。也就是说,静态方法只能调用静态方法和外部静态变量,但是非静态方法可以调用静态方法和静态变量。静态方法不存在覆盖。

  8. 在这里插入图片描述

  9. instanceof ,意思是前面是不是后面类的引用

  10. getClass(),getName(),得到引用的类,再给出类的名字

  11. 接口,interface。 public abstract interface 接口名字。 接口名字必须与文件名字一样,跟类是类似的。

  12. 接口结构
    在这里插入图片描述
    15.implements表示这个类用到了那些接口在这里插入图片描述

  13. Static and Non static
    在这里插入图片描述

  14. 内部类,用statci修饰的成员是属于类的,在static的方法里可以用类名直接调用;
    不用statci修饰的成员是属于具体实例对象的,需要用对象名调用,且在static的方法里不可以调用。
    在这里插入图片描述
    18 ,wrap box自动装箱和自动拆箱
    在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行:

Integer i = new Integer(10);创建的new Integer(10)保存在heap堆,i保存在stack但是指向heap,所以创建不同对象的时候,引用是不相等的
在这里插入图片描述
  而在从Java SE5开始就提供了自动装箱的特性,如果要生成一个数值为10的Integer对象,只需要这样就可以了:

Integer i = 10;
  这个过程中会自动根据数值创建对应的 Integer对象,这就是装箱。

那什么是拆箱呢?顾名思义,跟装箱对应,就是自动将包装器类型转换为基本数据类型:

Integer i = 10; //装箱
int n = i; //拆箱
  简单一点说,装箱就是 自动将基本数据类型转换为包装器类型;拆箱就是 自动将包装器类型转换为基本数据类型。

下表是基本数据类型对应的包装器类型:

int(4字节) Integer
byte(1字节) Byte
short(2字节) Short
long(8字节) Long
float(4字节) Float
double(8字节) Double
char(2字节) Character
boolean(未定) Boolean
当 "=="运算符的两个操作数都是 包装器类型的引用,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)

valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。
在这里插入图片描述

Another concept (although not related to this question but about static methods) is that static methods are never overridden. They are hidden just like static or non-static fields. For example,
class A{
int i = 10;
public static void m1(){ }
public void m2() { }
}
class B extends A{
int i = 20;
public static void m1() { }
public void m2() { }
}
Here, UNLIKE m2, m1() of B does not override m1() of A, it just hides m1() of A, as proven by the following code:
A a = new B();
System.out.println(a.i) //will print 10 instead of 20
a.m1(); //will call A’s m1
a.m2(); //will call B’s m2 as m2() is not static and so overrides A’s m2()

###–###
var declaration is allowed only inside method body and for loop declarations. It is not allowed to declareclass/instance fields, method parameters, or method return types.

###------------------------------------------------------#######################
Default constructor (having no arguments) is automatically created only if the class does not define any constructors. So as soon as //2 is inserted the default constructor will not be created.

  1. 继承和实现
    ou should know the following concepts for answering this type of questions: Steps to check for valid override First, check the method signature (i.e. method name and the parameter list). If the signature of the method in the subclass matches the signature of the method in the super class, then it could be a valid override, otherwise it is just an overloaded method. Note that signature does not include parameter names and parameter’s generic type specification. Second, if it is a potential override, check the generic type specification of the parameters. If the overriding method does not use a generic type specification for the parameter type, then it is valid. The reverse is not valid i.e. the overriding method is allowed to erase the generic type specification but is not allowed to add a generic type specification if the overridden method does not have it. If both the methods have a generic type specification, then the specification must match exactly. For example, if the overridden method has Set, then the overriding method can use Set or Set. But if overridden method has Set, then the overriding method must also have Set for a valid override. Third, if it is a potential override, check the return type. Java allows “covariant” returns, which means, the return type of the overriding method must be the same or be a subtype of the return type mentioned in the overridden method. Check the two return types without the generic type specification. If return type of the overriding method is covariant with respect to the return type of the overriding method (for example, ArrayList is covariant with List), then perform the same check including the generic type specification (for example, ArrayList is covariant with List<? extends CharSequence>). Don’t get confused by the presence of in the code. The same rules of overriding still apply. The T in is called as the “type” parameter. It is used as a place holder for whatever type is actually used while invoking the method. For example, if you call the method List<T< transform(List list) with List, T will be typed to String. Thus, it will return List. If, in another place, you call the same method with Integer, T will be typed to Integer and therefore, the return type of the method for that invocation will be List
    Type erasure of generic method parameters Remember that unlike arrays, generic collections are not reified, which means that all generic information is removed from the compiled class. Thus, Set and Set are converted to just Set by the compiler while generating the class file. This implies that two methods whose parameter types differ only on the type specification are not really different methods. For example, void m(Set cs), void m(Set s), and void m(Set o) are not different method signatures at all. If you remove the type specification, they all resolve to the same signature i.e. void m(Set x). Hence, if you put them in the same class, the resulting class file will have two methods with the exact same signature. This is obviously a problem and so, the compiler rejects the code.If you put one of them in a superclass and another in a subclass, then from the compiler’s perspective they constitute valid overloading, however, from the JVM’s perspective it is an override and the JVM will not respect the compile time method binding done by the compiler based on the generic type specification. That is why Java does not allow this either. The exception to this rule is that the overriding method is allowed to erase the generic type specification. For example, if the overridden method has Set, then the overriding method can use Set or Set. But if overridden method has Set, then the overriding method must also have Set for a valid override.
    Rule of Covariant Returns An overriding method (i.e. a sub class’s method) is allowed to return a sub-type of the type returned by the overridden method (i.e. super class’s method). So, first check whether the return type of the overriding method is a subtype. For example, if the overriding method returns List, the overridden method can return ArrayList but not Object. Next, you need to check the type specification of generic types. This is a bit complicated. To determine this, you must remember the following hierarchy of subtypes. Assuming that S is a sub type of T and <<< means “is a subtype of”,
    here are the two hierarchies:
Hierarchy 1 : A<S> <<< A<? extends S> <<< A<? extends T> Example: Since Integer is a subtype of Number, List<Integer> is a subtype of List<? extends Integer> and List<? extends Integer> is a subtype of A<? extends Number>. Thus, if an overridden method returns List<? extends Integer>, the overriding method can return List<Integer> but not List<Number> or List<? extends Number>.   
Hierarchy 2 : A<T> <<< A<? super T> <<< A<? super S> Example: List<Number> is a subtype of List<? super Number> and List<? super Number> is a subtype of A<? super Integer> Thus, if an overridden method returns List<? super Number>, the overriding method can return List<Number> but not List<Integer> or List<? super Integer>.  

It is important to understand that List is not a subtype of List even though Integer is a subtype of Number.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值