代码块:
普通代码块:使用{}包围的代码,所有数据的作用域只在{}范围内.
构造块:在方法外,类的内部,并且优先于构造方法被执行.
每次创建对象的时候都会调用.
静态代码块:使用 static 修饰并且定义在类中,随着类的加载而被调用
并且只调用一次.
同步代码块:(多线程再讲)
/*
代码块:
普通代码块:使用{}包围的代码,所有数据的作用域只在{}范围内.
构造块:在方法外,类的内部,并且优先于构造方法被执行.
每次创建对象的时候都会调用.
静态代码块:使用 static 修饰并且定义在类中,随着类的加载而被调用
并且只调用一次.
同步代码块:(多线程再讲)
*/
class Person{
private String name;
public Person(){
System.out.println("3.构造方法被执行....");
}
{
//构造块
System.out.println("2.构造块被执行......");
}
static{
//静态代码块
System.out.println("1.静态代码块被执行......");
}
}
class CodeDemo1 {
public static void main(String[] args) {
new Person();//静态代码块,构造块,构造方法
new Person();//构造块,构造方法
{
int x = 10;
//System.out.println(x);
System.out.println("4.普通代码块被执行......");
}
//System.out.println(x);
}
}
可变参数:
语法:返回值类型 方法名称(数据类型…参数名称){ }
可变参数一定要出现在参数列表的最后一位.
参数列表有且只有一个可变参数,并且一定要出现在参数列表的最后一位.
/*
可变参数:
语法:返回值类型 方法名称(数据类型…参数名称){ }
可变参数一定要出现在参数列表的最后一位.
参数列表有且只有一个可变参数,并且一定要出现在参数列表的最后一位.
*/
class Demo1 {
public static void main(String[] args) {
add("总薪水",888,5,4,6,20);
//System.out.println(result);
}
//求总的方法
public static void add(String message,int flag,int... num){//int[] num
int sum = 0;
System.out.println(flag);
for(int i = 0; i < num.length; i++){
sum+=num[i];
}
System.out.println(message + "=" + sum);
}
/*
public static int add(int a,int b){
return a+b;
}
public static int add(int a,int b,int c){
return a+b+c;
}
*/
}
递归:递归就是自己调用自己.
递归(recursion):递归满足2个条件
1)有反复执行的过程(调用自身)
2)有跳出反复执行过程的条件(递归出口)
/*
递归:递归就是自己调用自己.
1*2*3*4*5
*/
class DiGuiDemo {
public static void main(String[] args) {
//System.out.println(forDemo(5));
System.out.println(diGui(5));
}
public static int diGui(int num){
if (num == 1){
return 1;
}
return num*diGui(num-1);
}
public static int forDemo(int num){
int ji = 1;
for (int i = 1; i <= num; i++){
ji*=i;
}
return ji;
}
}
内部类:就是类的里面还有一个类
访问规则:
1.内部类可以直接访问外部类的成员,因为内部类持有外部类的引用
该引用的写法:外部类名.this Outer.this
2.外部类要访问内部类的成员,必须先持有内部类的对象
访问限制:
一.当内部类被定义在成员位置上:
1.非私有(private)可以在外部其他类中直接创建内部类对象
外部类名.内部类名 对象名 = new 外部类名().new 内部类名();
Outer.Inner in = new Outer().new Inner();
2.被成员修饰符修饰(private static)
在外部其他类中如何访问静态内部类中的非静态成员
new Outer.Inner().innerSay();
在外部其他类中如何访问静态内部类中的静态成员
Outer.Inner.innerSay()
二.当内部类被定义在局部时
还可以直接访问外部类的成员,因为还持有外部类的引用.
不可以直接访问它所在的局部中的变量,只能访问被 final 修饰的变量
/*
成员(非静态)可以访问静态
静态只能访问静态
在类加载的时候,静态是先进内存的.
this代表当前对象
static是属于类的,它是随着类的加载而加载,并且优先于对象存在.
内部类:就是类的里面还有一个类
访问规则:
1.内部类可以直接访问外部类的成员,因为内部类持有外部类的引用
该引用的写法:外部类名.this Outer.this
2.外部类要访问内部类的成员,必须先持有内部类的对象
访问限制:
一.当内部类被定义在成员位置上:
1.非私有(private)可以在外部其他类中直接创建内部类对象
外部类名.内部类名 对象名 = new 外部类名().new 内部类名();
Outer.Inner in = new Outer().new Inner();
2.被成员修饰符修饰(private static)
在外部其他类中如何访问静态内部类中的非静态成员
new Outer.Inner().innerSay();
在外部其他类中如何访问静态内部类中的静态成员
Outer.Inner.innerSay()
二.当内部类被定义在局部时
还可以直接访问外部类的成员,因为还持有外部类的引用.
不可以直接访问它所在的局部中的变量,只能访问被 final 修饰的变量
*/
class Outer{
static int a = 10;
static class Inner{
//int a = 20;
static void innerSay(){
//int a = 30;
System.out.println("a : " + a);
}
}
void outSay(){
Inner in = new Inner();
in.innerSay();
//new Inner().innerSay();
}
}
class InnerDemo1 {
public static void main(String[] args) {
//外部类名.内部类名 对象名 = new 外部类名().new 内部类名();
//Outer.Inner in = new Outer().new Inner();
//in.innerSay();
//实例化 Scanner 对象,对象名叫 input
//Scanner input = new Scanner(System.in);
//实例化一个 Outer 对象,对象名叫 out
//Outer ccc = new Outer();
//new Outer().outSay();
//new Outer.Inner().innerSay();
Outer.Inner.innerSay();
}
}
class Person{
void say(){}
Math.max(43,23);
}
class Outer{
int x = 10;
void method(){
final int y = 100;
class Inner{
void say(){
System.out.println("y = " + y);
}
}
new Inner().say();
}
}
class InnerDemo2 {
public static void main(String[] args) {
new Outer().method();
}
}
链表:
//节点管理类
class NodeManager{
private Node root;//表示根节点
//添加节点
public void addNode(String name){
if (root == null){
root = new Node(name);
}else{
root.add(name);
}
}
//删除节点
public void delNode(String name){
if (root!=null){
if (root.name.equals(name)){
root = root.next;
}else{
root.del(name);
}
}
}
//输出所有节点
public void printNode(){
if (root != null){
System.out.print(root.name);
root.print();
System.out.println();
}
}
class Node{
private String name;//表示节点名称(数据)
private Node next;//表示链表的下一个节点
public Node(String name){
this.name = name;
}
//添加节点
public void add(String name){//节点2
if (this.next == null){
this.next = new Node(name);
}else{
this.next.add(name);
}
}
//删除节点
public void del(String name){
if (this.next!=null){
if (this.next.name.equals(name)){
this.next = this.next.next;
}else{
this.next.del(name);
}
}
}
//输出所有节点
public void print(){
if (this.next != null){
System.out.print("-->"+this.next.name);
this.next.print();
}
}
}
}
class LinkDemo {
public static void main(String[] args) {
NodeManager nm = new NodeManager();
nm.addNode("节点1");
nm.addNode("节点2");
nm.addNode("节点3");
nm.addNode("节点4");
nm.addNode("节点5");
nm.addNode("节点6");
nm.addNode("节点7");
nm.printNode();
nm.delNode("节点7");
nm.printNode();
}
}
static静态:
先给出一个明显的使用限制: · 在类中使用 static 关键字声明的方法只能调用 static 声明的属性或方法,而不能调用非 static 声明的属性或方法。