Java basics at a glance (will be added continuously)

static

Static variables belong to classes
Ordinary variables and methods belong to objects
Static methods cannot use non-static methods
Ordinary variables can use static methods


Execute the static initialization block of the parent class first. The static
initialization block is the initialization class


package
must be placed in the first sentence


Java.lang is a core package

import--import----The shortcut key for automatic package import in Java is ctrl+shift+O


Three characteristics of object-oriented: inheritance; encapsulation; polymorphism

The interface that inherits
Java can be multi-inherited. The Java class has only single inheritance . The
subclass inherits the parent class, and you can get all the properties and methods of the parent class except the constructor of the parent class.
Java.lang.object is the ancestor class of all classes.
Select the class and press ctrl +t can directly open the inheritance tree to view the inheritance relationship
instanceof operator
(object instanceof class)


Overriding (and overriding)
the method of the subclass replaces the method of the parent class.
Keep the method name and parameter list the same when overriding. The
return value type of the subclass is smaller than
that of the parent class. Big


The method in the class of object
1.to String
2.equals method (right-click on the screen and click on the source to construct the method directly)

super is located at the top of the construction method. When the construction method is executed, the super and parent class methods will be executed first.
If the parent class is object, it will be empty
. When programming, high cohesion and low coupling will be pursued.


There are four encapsulated
access control characters : 1.private: Only oneself can use it, indicating that only oneself can access privately. 2.default: It means that there is no modifier modification, and only classes in the same package can access it. 3.protected: It means that it can be accessed by the same package. Access by subclasses in a package and other packages 4.public: indicates that it can be accessed by all classes in the project




 

 

Polymorphism
means that calls to the same method may behave differently depending on the object


Three necessary conditions for polymorphism
1. Inheritance
2. Method overriding
3. Parent class reference points to child class object (*****)


final modifies constants, variables, methods, and classes.

Modified variable: Once final modified variable cannot be changed, once initial value is assigned, it cannot be reassigned
Modified method: This method cannot be overridden by subclasses, but can be overloaded
Modified class: Modified class cannot be inherited

 

Abstract class: keyword (abstract)
method: declaration plus implementation
Abstract methods must be defined in the abstract class.
An abstract class can define common methods and common properties
A class with abstract methods must be an abstract class.
An abstract class cannot be instantiated and cannot be instantiated with new. An
abstract class can contain properties/methods/constructors, but the constructor cannot be To new instances can only be used to be called by subclasses and
abstract classes can only be used to inherit.
Abstract methods must be implemented by subclasses.

 

interface

For example: the interface can be regarded as the company's regulations, and the class is the company's employees, subject to
the constraints of various specifications, and the class can implement the functions of multiple interfaces.

 

You can build an interface directly in the package

There are only constants and abstract methods in an interface

设计和实现分离利于分工
在多人协作时使用


接口可以描述更加抽象的东西
进行更加抽象的定义
一个类可以实现多个接口

通过implements来实现

接口支持多继承

 

回调(CallBack)
多态的应用

 

superinterface
(1. 超接口
l 从直接的超接口( superinterface )中继承的成员。
blog.csdn.net|基于18个网页
2.
超级接口
超级接口(superinterface)从本质上来说一定还是一个接口。|评论
zhidao.baidu.com|基于4个网页
3.
超级界面
他没有任何方法,是所有消息的超级界面(superinterface)。
所有消息必须符合这个界面,相应的符合Serializable。)


内部类
作用:
1.内部类提供了更好的封装。只能让外部类“直接访问”。不允许同一个
包中的其他类直接访问。
2.内部类可以直接访问外部类的私有属性,内部类被当做其外部类的成员,
但外部类不能访问内部类的内部属性。

 

面向对象与面向过程
面向对象是用设计的眼光来看待问题(object)
对象就是在内存中的内存块
对象中会放一些变量及方法

类(class)可以看作是对象的模板称为对象的抽象
抽出类似的部分去定义一个类

通过抽象来创建一个类,程序通过类再产生对象
对象(object)(instance)
类中要有属性与方法
属性表示静态特征,及成员变量
方法的定义方式【修饰符】 方法返回值 方法名(){ }

类中还有构造方法,作用为初始化对象。
创建对象:类名 对象名=new 类名();

类名 对象名;//创建一个累的对象


Scanner
获取你输入的数据,用之前需要将 import java.util.Scanner;这个包导入

 


包装类与基本数据类型相对应一共有八种
包装类位于Java.lang包
实例:Integer 对象=new Integer(1000);
将1000包装成为一个对象。
Integer extends number类
Integer.toHexString():将i变为16进制
Integer.parseIn():将字符串转为数字


自动拆箱自动装箱是编译器做的


数组:

 

 

File类
separator:分隔符

 


异常机制,当遇到空指针的异常机制时,加上一个IF条件语句,让异常消失。


异常的处理方法之一
捕获异常(tyr,catch,finally)
try:格式:try{
//语句一;
//语句二;
//语句三;
}catch(Exceptione e){
//语句四;
}finally{
//语句五;
}


异常处理机制使用时一般将子类放前面,父类放后面。


写try—catch语句时的快捷键右键单击选定surround with,然后选Try/catch Block
会自动添加异常处理机制
补充内容:try/catch/finally/return的执行顺序:
1.执行try/catch,给返回值
2.执行 finally
3.return


声明异常throw处理异常的第二个方法
增加一个抛出的声明

关掉try/catch的是read.close

查看层次结构的方式——单击右键找到open Call Hierarchy
快捷键为——ctrl+alt+h即可查看层次结构

 

collection(容器)(也称集合框架)
实例如下:package cn.wk.collection;

import java.awt.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Collection_Demo {

public static void main(String[] args) {

Collection<String> coll01 = new ArrayList<String>();
coll01.add("bbbbbb1");
coll01.add("bbbbbb2");
coll01.add("bbbbbb3");
coll01.add("bbbbbb4");

coll01.size();
System.out.println(coll01);

//迭代器的使用1
Iterator it=coll01.iterator();
for(Iterator it1=coll01.iterator();it1.hasNext();) {
String str=(String) it1.next();
System.out.println(str+" ");
}
System.out.println(coll01.size());
boolean b1=coll01.remove("bbbbbb2");
System.out.println(b1);
System.out.println(coll01);

//添加一个集合
Collection coll02=new ArrayList();

coll02.addAll(coll01);
System.out.println(coll02);
}
}

 

增强的for循环
代码如下:
for(int m:a){

system.out.println(m);

}

用来遍历数组的元素。

 


IO流

概念:流是一组有顺序的,有起点和终点的字节集合,
是对数据传输的总称或抽象。即数据在两设备间的传输称为流。
流的本质是数据传输,根据数据传输特性将流抽象为各种类,
方便更直观的进行数据操作。
IO流的分类:根据处理数据类型的不同分为:字符流和字节流;
根据数据流向不同分为:输入流和输出流。

 

使用流程

一、
建立联系
File对象,源头
二、
选择流

文件输入流InputStream FileOutStream
三、
操作 write()+flash
四、
释放资源 关闭

详述请见网址:http://www.cnblogs.com/oubo/archive/2012/01/06/2394638.html,


字符流的两个顶层父类
1.Reader ,Writer


这些体系的子类,都以父类为后缀
而且子类名的前缀就是该对象的功能

文字为字符串使用与他关系最近的字符流
例:

//需求:将一些文件储存到硬盘的的一个文件中
记住:如果要操作文字数据,建议优先考虑字符流。
而且要将数据从内存写到硬盘上,要使用字符流中的输出流。Writer


流的规律
转换流:
INPUTSTREAM: 字节到字符的桥梁。解码

OUTPUTSTREAM: 字符到字节的桥梁。编码


流的操作规律:
之所以要弄清楚这个规律,因为流太多,不知道该用哪个

四个明确
1.明确源和目的(汇)
源:input stream reader
目的:output stream writer

2.明确数据是否是纯文本数据。
源:是纯文本数据:reader
否:input stream
目的是:writer
否:output stream

到这里可以明确具体使用哪个体系。

3.明确具体的设备。
源设备:硬盘:file
键盘:system。in
内存:数组
网络:socket流


目的设备:
硬盘:file
控制台:system。out
内存:数组
网络:socket流。

4.是否需要其他额外功能流对象。
1.是否需要高效(缓冲区)
是,就加上buffer。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324865570&siteId=291194637