<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://pyrans.github.io/atom.xml" rel="self" type="application/atom+xml" /><link href="https://pyrans.github.io/" rel="alternate" type="text/html" /><updated>2021-08-12T02:59:47+00:00</updated><id>https://pyrans.github.io/atom.xml</id><title type="html">Pyrans’ Blog</title><subtitle>Frozen three feet Day cold</subtitle><author><name>Pyrans</name></author><entry><title type="html">Java类和对象</title><link href="https://pyrans.github.io/java/2021/01/27/%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1/" rel="alternate" type="text/html" title="Java类和对象" /><published>2021-01-27T00:00:00+00:00</published><updated>2021-01-27T00:00:00+00:00</updated><id>https://pyrans.github.io/java/2021/01/27/%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1</id><content type="html" xml:base="https://pyrans.github.io/java/2021/01/27/%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1/">&lt;h2 id=&quot;类和对象&quot;&gt;类和对象&lt;/h2&gt;

&lt;h4 id=&quot;类的定义&quot;&gt;类的定义&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;类的重要性：是Java程序的基本组成单位&lt;/li&gt;
  &lt;li&gt;类是什么：是对现实生活中一类具有共同属性和行为的事物的抽象，确定对象将会拥有的属性和行为&lt;/li&gt;
  &lt;li&gt;类的组成：属性和行为&lt;/li&gt;
  &lt;li&gt;属性：在类中通过成员变量来体现（类中方法外的变量）
行为：在类中通过成员方法来体现（和方法相比去掉static关键字即可）&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;类名&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// 成员变量&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;变量1的数据类型&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;变量1&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;；&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;变量2的数据类型&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;变量2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
		  &lt;span class=&quot;c1&quot;&gt;// 成员方法&lt;/span&gt;
		  &lt;span class=&quot;n&quot;&gt;方法1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
		  &lt;span class=&quot;n&quot;&gt;方法2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
		  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;对象的使用&quot;&gt;对象的使用&lt;/h4&gt;

&lt;p&gt;创建对象：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;格式：类名 对象名 = new 类名();&lt;/li&gt;
  &lt;li&gt;范例: Phone p = new Phone();&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;使用对象:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;使用成员变量
    &lt;ul&gt;
      &lt;li&gt;格式：对象名.变量名&lt;/li&gt;
      &lt;li&gt;范例p.number&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;使用成员方法
    &lt;ul&gt;
      &lt;li&gt;格式：对象名.方法名()&lt;/li&gt;
      &lt;li&gt;范例：p.call()&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;成员变量与局部变量&quot;&gt;成员变量与局部变量&lt;/h2&gt;

&lt;p&gt;成员变量：类中方法外的变量&lt;/p&gt;

&lt;p&gt;局部变量：方法中的变量&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;&lt;strong&gt;区别&lt;/strong&gt;&lt;/th&gt;
      &lt;th&gt;&lt;strong&gt;成员变量&lt;/strong&gt;&lt;/th&gt;
      &lt;th&gt;&lt;strong&gt;局部变量&lt;/strong&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;类中位置不同&lt;/td&gt;
      &lt;td&gt;类中方法外&lt;/td&gt;
      &lt;td&gt;方法内或者方法声明上&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;内存中位置不同&lt;/td&gt;
      &lt;td&gt;堆内存&lt;/td&gt;
      &lt;td&gt;栈内存&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;生命周期不同&lt;/td&gt;
      &lt;td&gt;随着对象的存在而存在，随着对象的消失而消失&lt;/td&gt;
      &lt;td&gt;随着方法的调用而存在，随着方法的调用完毕而消失&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;初始化值不同&lt;/td&gt;
      &lt;td&gt;有默认的初始化值&lt;/td&gt;
      &lt;td&gt;没有默认的初始化值，必须先定义，赋值，才能使用&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;封装&quot;&gt;封装&lt;/h2&gt;

&lt;h4 id=&quot;private关键字&quot;&gt;private关键字&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;private是一个权限修饰符&lt;/li&gt;
  &lt;li&gt;保护成员不被别的类使用，被修饰的成员只能在本类中被访问
    &lt;ul&gt;
      &lt;li&gt;针对private修饰的成员变量，如果需要被其他类使用，提供相应的操作：
        &lt;ul&gt;
          &lt;li&gt;提供“get变量名()”方法，用于获取成员变量的值，方法用public修饰&lt;/li&gt;
          &lt;li&gt;提供“set变量名(参数)”方法，用于设置成员变量的值，方法用public修饰&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;private的使用
    &lt;ul&gt;
      &lt;li&gt;一个标准类的编写：
        &lt;ul&gt;
          &lt;li&gt;把成员变量用private修饰&lt;/li&gt;
          &lt;li&gt;提供对应的get变量名()/set变量名()方法&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;this关键字&quot;&gt;this关键字&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;this修饰的变量用于指代成员变量
    &lt;ul&gt;
      &lt;li&gt;方法的形参如果与成员变量同名，不带this修饰的变量指的是形参，而不是成员变量&lt;/li&gt;
      &lt;li&gt;方法的形参没有与成员变量同名，不带this修饰的变量指的是成员变量&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;public class Student{
	pricate String name;
	
	public String getName(){
		return name;
	}
	public void setName(String name){
		// this.name 指定成员变量 将name复制给Student的成员变量name
		this.name = name
	}
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;封装-1&quot;&gt;封装&lt;/h4&gt;

&lt;ol&gt;
  &lt;li&gt;封装概述：
    &lt;ul&gt;
      &lt;li&gt;是面向对象三大特征之一（封装，继承，多态）&lt;/li&gt;
      &lt;li&gt;是面向对象编程语言对客观世界的模拟，客观世界里成员变量都是隐藏在对象内部的，外界是无法直接操作的&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;封装原则：
    &lt;ul&gt;
      &lt;li&gt;将类的某些信息隐藏在类内部，不允许外部程序直接访问，而是通过该类提供的方法来实现对隐藏信息的操作和访问&lt;/li&gt;
      &lt;li&gt;成员变量private，提供对应的getXxx()/setXxx()方法&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;封装好处：
    &lt;ul&gt;
      &lt;li&gt;通过方法来控制成员变量的操作，提高了代码的安全性&lt;/li&gt;
      &lt;li&gt;把代码用方法进行封装，提高了代码的复用性&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;构造方法&quot;&gt;构造方法&lt;/h2&gt;

&lt;p&gt;构造方法是类的一种特殊的方法，作用是初始化对象。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;格式：
public class 类名{
        修饰符 类名( 参数 ) {
        }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;构造方法的注意事项：&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;构造方法的创建
    &lt;ul&gt;
      &lt;li&gt;如果没有定义构造方法，系统将给出一个默认的无参数构造方法&lt;/li&gt;
      &lt;li&gt;如果定义了构造方法，系统将不再提供默认的构造方法&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;构造方法的重载
    &lt;ul&gt;
      &lt;li&gt;如果自定义了带参构造方法，还要使用无参数构造方法，就必须再写一个无参数构造方法&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;推荐的使用方式
    &lt;ul&gt;
      &lt;li&gt;无论是否使用，都手工书写无参数构造方法&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;创建一个标准类：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;public class People{
	private String name;
	private int age;
	public People(){}
	public People(String name,int age){
		this.name = name;
		this.age = age;
	}
	public String getName(){
		return name;
	}
	public int getAge(){
		retuen age;
	}
	public void setName(String name){
		this.name = name;
	}
	public void setAge(int age){
		this.age = age;
	}
	public void show(){
		System.out.println(name + &quot;,&quot; + age);
	}
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Pyrans</name></author><category term="Java" /><category term="Java类和对象" /><summary type="html">类和对象 类的定义 类的重要性：是Java程序的基本组成单位 类是什么：是对现实生活中一类具有共同属性和行为的事物的抽象，确定对象将会拥有的属性和行为 类的组成：属性和行为 属性：在类中通过成员变量来体现（类中方法外的变量） 行为：在类中通过成员方法来体现（和方法相比去掉static关键字即可） public class 类名 { // 成员变量 变量1的数据类型 变量1； 变量2的数据类型 变量2; ... // 成员方法 方法1; 方法2; ... } 对象的使用 创建对象： 格式：类名 对象名 = new 类名(); 范例: Phone p = new Phone(); 使用对象: 使用成员变量 格式：对象名.变量名 范例p.number 使用成员方法 格式：对象名.方法名() 范例：p.call() 成员变量与局部变量 成员变量：类中方法外的变量 局部变量：方法中的变量 区别 成员变量 局部变量 类中位置不同 类中方法外 方法内或者方法声明上 内存中位置不同 堆内存 栈内存 生命周期不同 随着对象的存在而存在，随着对象的消失而消失 随着方法的调用而存在，随着方法的调用完毕而消失 初始化值不同 有默认的初始化值 没有默认的初始化值，必须先定义，赋值，才能使用 封装 private关键字 private是一个权限修饰符 保护成员不被别的类使用，被修饰的成员只能在本类中被访问 针对private修饰的成员变量，如果需要被其他类使用，提供相应的操作： 提供“get变量名()”方法，用于获取成员变量的值，方法用public修饰 提供“set变量名(参数)”方法，用于设置成员变量的值，方法用public修饰 private的使用 一个标准类的编写： 把成员变量用private修饰 提供对应的get变量名()/set变量名()方法 this关键字 this修饰的变量用于指代成员变量 方法的形参如果与成员变量同名，不带this修饰的变量指的是形参，而不是成员变量 方法的形参没有与成员变量同名，不带this修饰的变量指的是成员变量 public class Student{ pricate String name; public String getName(){ return name; } public void setName(String name){ // this.name 指定成员变量 将name复制给Student的成员变量name this.name = name } } 封装 封装概述： 是面向对象三大特征之一（封装，继承，多态） 是面向对象编程语言对客观世界的模拟，客观世界里成员变量都是隐藏在对象内部的，外界是无法直接操作的 封装原则： 将类的某些信息隐藏在类内部，不允许外部程序直接访问，而是通过该类提供的方法来实现对隐藏信息的操作和访问 成员变量private，提供对应的getXxx()/setXxx()方法 封装好处： 通过方法来控制成员变量的操作，提高了代码的安全性 把代码用方法进行封装，提高了代码的复用性 构造方法 构造方法是类的一种特殊的方法，作用是初始化对象。 格式： public class 类名{ 修饰符 类名( 参数 ) { } } 构造方法的注意事项： 构造方法的创建 如果没有定义构造方法，系统将给出一个默认的无参数构造方法 如果定义了构造方法，系统将不再提供默认的构造方法 构造方法的重载 如果自定义了带参构造方法，还要使用无参数构造方法，就必须再写一个无参数构造方法 推荐的使用方式 无论是否使用，都手工书写无参数构造方法 创建一个标准类： public class People{ private String name; private int age; public People(){} public People(String name,int age){ this.name = name; this.age = age; } public String getName(){ return name; } public int getAge(){ retuen age; } public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; } public void show(){ System.out.println(name + &quot;,&quot; + age); } }</summary></entry><entry><title type="html">Java方法</title><link href="https://pyrans.github.io/java/2021/01/15/JAVA%E6%96%B9%E6%B3%95(%E5%87%BD%E6%95%B0)/" rel="alternate" type="text/html" title="Java方法" /><published>2021-01-15T00:00:00+00:00</published><updated>2021-01-15T00:00:00+00:00</updated><id>https://pyrans.github.io/java/2021/01/15/JAVA%E6%96%B9%E6%B3%95(%E5%87%BD%E6%95%B0)</id><content type="html" xml:base="https://pyrans.github.io/java/2021/01/15/JAVA%E6%96%B9%E6%B3%95(%E5%87%BD%E6%95%B0)/">&lt;h2 id=&quot;方法定义&quot;&gt;方法定义&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;方法：方法是具有独立功能的代码块组织成为一个整体，使其具有特殊功能的代码集&lt;/li&gt;
  &lt;li&gt;注意：
    &lt;ul&gt;
      &lt;li&gt;方法必须先创建才能使用，该过程成为方法定义&lt;/li&gt;
      &lt;li&gt;方法创建后需要手动使用后才执行，即方法调用&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;不带参方法&quot;&gt;不带参方法&lt;/h4&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;定义&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;方法名&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;方法体&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;举个栗子:&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hello world!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;调用&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// hello world!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;带参方法&quot;&gt;带参方法&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;定义：
// 参数中的数据类型与变量名都不能缺少
// 多个参数之间使用逗号分隔
public static void 方法名(参数){
	方法体;
}

调用：
方法名(参数);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;举个栗子：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;单个参数：
定义：
public static void get_number(int a){
	System.out.println(a);
}

调用:
get_number(5); // 5


多个参数:
定义：
public static void get_max(int a, int b){
	if(a &amp;gt; b){
		System.out.println(a);
	}else{
	System.out.println(a);
	}
}

调用:
get_max(10, 9); // 10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;带返回值&quot;&gt;带返回值&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;注意:方法的返回值通常会使用变量接收，否则该返回值将无意义&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;定义：
public static 数据类型 方法名(参数){
	方法体;
	return 数据;
}

举例：
public static int get_max(int a, int b){
	if(a &amp;gt; b){
		return a;
	}else{
        return b;
	}
}

第二个例子:
public static boolean is_number(int a){
	return true ;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;void表示无返回值，可以省略return，也可以单独的书写return，后面不加数据。&lt;/p&gt;

&lt;h2 id=&quot;方法重载&quot;&gt;方法重载&lt;/h2&gt;

&lt;p&gt;方法重载指同一个类中定义的多个方法之间的关系，满足下列条件的多个方法相互构成重载&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;多个方法在同一个类中&lt;/li&gt;
  &lt;li&gt;多个方法具有相同的方法名&lt;/li&gt;
  &lt;li&gt;多个方法的参数不相同，类型不同或者数量不同&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;栗子1：
public class MethodDemo {
	public static void fn(int a){
	方法体;
    }
    // 重载
    public static int fn(double a){
    方法体;
    }
}


栗子2：
public class MethodDemo {
	public static float fn(int a) {
	方法体;
    }
    public static int fn(int a , int b) {
    方法体;
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Dilank</name></author><category term="Java" /><category term="Java基础" /><summary type="html">方法定义 方法：方法是具有独立功能的代码块组织成为一个整体，使其具有特殊功能的代码集 注意： 方法必须先创建才能使用，该过程成为方法定义 方法创建后需要手动使用后才执行，即方法调用 不带参方法 定义： public static void 方法名(){ 方法体; } 举个栗子: public static void hello(){ System.out.println(&quot;hello world!&quot;); } 调用： hello(); // hello world! 带参方法 定义： // 参数中的数据类型与变量名都不能缺少 // 多个参数之间使用逗号分隔 public static void 方法名(参数){ 方法体; } 调用： 方法名(参数); 举个栗子： 单个参数： 定义： public static void get_number(int a){ System.out.println(a); } 调用: get_number(5); // 5 多个参数: 定义： public static void get_max(int a, int b){ if(a &amp;gt; b){ System.out.println(a); }else{ System.out.println(a); } } 调用: get_max(10, 9); // 10 带返回值 注意:方法的返回值通常会使用变量接收，否则该返回值将无意义 定义： public static 数据类型 方法名(参数){ 方法体; return 数据; } 举例： public static int get_max(int a, int b){ if(a &amp;gt; b){ return a; }else{ return b; } } 第二个例子: public static boolean is_number(int a){ return true ; } void表示无返回值，可以省略return，也可以单独的书写return，后面不加数据。 方法重载 方法重载指同一个类中定义的多个方法之间的关系，满足下列条件的多个方法相互构成重载 多个方法在同一个类中 多个方法具有相同的方法名 多个方法的参数不相同，类型不同或者数量不同 栗子1： public class MethodDemo { public static void fn(int a){ 方法体; } // 重载 public static int fn(double a){ 方法体; } } 栗子2： public class MethodDemo { public static float fn(int a) { 方法体; } public static int fn(int a , int b) { 方法体; } }</summary></entry><entry><title type="html">Java数组</title><link href="https://pyrans.github.io/java/2021/01/14/JAVA%E6%95%B0%E7%BB%84/" rel="alternate" type="text/html" title="Java数组" /><published>2021-01-14T00:00:00+00:00</published><updated>2021-01-14T00:00:00+00:00</updated><id>https://pyrans.github.io/java/2021/01/14/JAVA%E6%95%B0%E7%BB%84</id><content type="html" xml:base="https://pyrans.github.io/java/2021/01/14/JAVA%E6%95%B0%E7%BB%84/">&lt;h2 id=&quot;什么是数组&quot;&gt;什么是数组&lt;/h2&gt;

&lt;p&gt;数组(array)是一种用于存储多个相同类型的存储模型。&lt;/p&gt;

&lt;h2 id=&quot;数组定义&quot;&gt;数组定义&lt;/h2&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;格式一&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;数据类型&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;变量名&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;格式二&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;数据类型&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;变量名&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;数组初始化&quot;&gt;数组初始化&lt;/h2&gt;

&lt;p&gt;Java中的数组必须先初始化,然后才能使用
所谓初始化：就是为数组中的数组元素分配内存空间，并为每个数组元素赋值&lt;/p&gt;

&lt;h4 id=&quot;动态初始化&quot;&gt;动态初始化&lt;/h4&gt;

&lt;p&gt;动态初始化：初始化时只指定数组长度，由系统为数组分配初始值&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;格式&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;数据类型&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;变量名&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;数据类型&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;数组长度&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;举例:&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;静态初始化&quot;&gt;静态初始化&lt;/h4&gt;

&lt;p&gt;静态初始化：初始化时指定每个数组元素的初始值，由系统决定数组长度&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;格式一&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;数据类型&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;变量名&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;数据类型&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;数据1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;数据2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;......};&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;举例:&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;格式二&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;数据类型&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;变量名&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;数据1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;数据2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;......};&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;举例:&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;数组元素访问&quot;&gt;数组元素访问&lt;/h2&gt;

&lt;pre&gt;&lt;code class=&quot;language-Java&quot;&gt;数组变量访问直接调用数组名即可。

数组内部保存的数据访问方式：
数组名[索引]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;注&lt;/strong&gt;：索引是数组中数据的编号方式&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;作用：索引用于访问数组中的数据使用，数组名[索引]等同于变量名，是一种特殊的变量名&lt;/li&gt;
  &lt;li&gt;特征①：索引从0开始&lt;/li&gt;
  &lt;li&gt;特征②：索引是连续的&lt;/li&gt;
  &lt;li&gt;特征③：索引逐一增加，每次加1&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;数组常见操作&quot;&gt;数组常见操作&lt;/h2&gt;

&lt;h4 id=&quot;数组遍历&quot;&gt;数组遍历&lt;/h4&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;遍历数组一般使用循环语句&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;举个栗子&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 定义数组&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 使用for循环遍历数组&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++){&lt;/span&gt;
	&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;获取数组长度&quot;&gt;获取数组长度&lt;/h4&gt;

&lt;p&gt;数组长度: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arr.length&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;举个栗子&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 定义数组&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 结果为10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;获取最值&quot;&gt;获取最值&lt;/h4&gt;

&lt;p&gt;举个栗子：&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 定义数组&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;58&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;88&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 定义变量用于接收最值,这里以最小值举例&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++){&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 循环结束，打印最值&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 结果为0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Dilank</name></author><category term="Java" /><category term="Java基础" /><summary type="html">什么是数组 数组(array)是一种用于存储多个相同类型的存储模型。 数组定义 格式一： 数据类型[] 变量名 格式二： 数据类型 变量名[] 数组初始化 Java中的数组必须先初始化,然后才能使用 所谓初始化：就是为数组中的数组元素分配内存空间，并为每个数组元素赋值 动态初始化 动态初始化：初始化时只指定数组长度，由系统为数组分配初始值 格式： 数据类型[] 变量名 = new 数据类型[数组长度]; 举例: int[] arr0 = new int[10]; 静态初始化 静态初始化：初始化时指定每个数组元素的初始值，由系统决定数组长度 格式一： 数据类型[] 变量名 = new 数据类型[]{数据1, 数据2, ......}; 举例: int[] arr0 = new int[]{1, 2, 3}; 格式二： 数据类型[] 变量名 = {数据1, 数据2, ......}; 举例: int[] arr0 = {1, 2, 3}; 数组元素访问 数组变量访问直接调用数组名即可。 数组内部保存的数据访问方式： 数组名[索引] 注：索引是数组中数据的编号方式 作用：索引用于访问数组中的数据使用，数组名[索引]等同于变量名，是一种特殊的变量名 特征①：索引从0开始 特征②：索引是连续的 特征③：索引逐一增加，每次加1 数组常见操作 数组遍历 遍历数组一般使用循环语句，举个栗子： // 定义数组 int[] arr_num = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; // 使用for循环遍历数组 for(int i=0;i&amp;lt;10;i++){ System.out.println(arr_num[i]); } 获取数组长度 数组长度: arr.length 举个栗子： // 定义数组 int[] arr_num = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; System.out.println(arr_num.length) // 结果为10 获取最值 举个栗子： // 定义数组 int[] arr_num = {10, 24, 11, 4, 58, 3, 88, 1, 5, 0}; // 定义变量用于接收最值,这里以最小值举例 int min = arr_num[0]; for(int i=1;i&amp;lt;arr_num.length;i++){ if(min &amp;gt; arr_num[i]){ min = arr_num[i]; } } // 循环结束，打印最值 System.out.println(min); // 结果为0</summary></entry><entry><title type="html">Java基础环境</title><link href="https://pyrans.github.io/java/2020/08/28/JAVA%E5%9F%BA%E7%A1%80%E7%8E%AF%E5%A2%83/" rel="alternate" type="text/html" title="Java基础环境" /><published>2020-08-28T00:00:00+00:00</published><updated>2020-08-28T00:00:00+00:00</updated><id>https://pyrans.github.io/java/2020/08/28/JAVA%E5%9F%BA%E7%A1%80%E7%8E%AF%E5%A2%83</id><content type="html" xml:base="https://pyrans.github.io/java/2020/08/28/JAVA%E5%9F%BA%E7%A1%80%E7%8E%AF%E5%A2%83/">&lt;h2 id=&quot;java基础环境介绍&quot;&gt;JAVA基础环境介绍&lt;/h2&gt;

&lt;h4 id=&quot;jvm&quot;&gt;JVM&lt;/h4&gt;

&lt;p&gt;JVM是Java Virtual Machine（Java&lt;a href=&quot;https://baike.baidu.com/item/虚拟机&quot;&gt;虚拟机&lt;/a&gt;）的缩写，JVM是一种用于计算设备的规范，它是一个虚构出来的计算机，是通过在实际的计算机上仿真模拟各种计算机功能来实现的&lt;/p&gt;

&lt;h4 id=&quot;jre&quot;&gt;JRE&lt;/h4&gt;

&lt;p&gt;是Java程序的运行时环境，包含 &lt;strong&gt;JVM&lt;/strong&gt; 和运行时所需要的核心类库。 我们想要&lt;strong&gt;运行&lt;/strong&gt;一个已有的Java程序，那么只需安装 &lt;strong&gt;JRE&lt;/strong&gt; 即可。&lt;/p&gt;

&lt;h4 id=&quot;jdk&quot;&gt;JDK&lt;/h4&gt;

&lt;p&gt;是Java程序开发工具包，&lt;strong&gt;包含&lt;/strong&gt; &lt;strong&gt;JRE&lt;/strong&gt; 和开发人员使用的工具。其中的开发工具：编译工具（&lt;strong&gt;javac.exe&lt;/strong&gt;）和运行工具（&lt;strong&gt;java.exe&lt;/strong&gt;）。我们想要&lt;strong&gt;开发&lt;/strong&gt;一个全新的Java程序，那么必须安装 &lt;strong&gt;JDK&lt;/strong&gt; 。&lt;/p&gt;

&lt;h4 id=&quot;jdkjre与jvm的关系&quot;&gt;JDK、JRE与JVM的关系&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/Pyrans/Pyrans.github.io/blob/master/img/2020082901.PNG?raw=true&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;java基础环境安装&quot;&gt;JAVA基础环境安装&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;1.下载jdk 	通过官网下载获取 &lt;a href=&quot;https://www.oracle.com/&quot;&gt;https://www.oracle.com/&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;安装，傻瓜式安装，安装路径中不要包含中文和空格，建议所有的开发工具安装目录统一&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;jdk的安装目录&lt;/p&gt;

    &lt;table&gt;
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;th style=&quot;text-align: left&quot;&gt;&lt;strong&gt;目录名称&lt;/strong&gt;&lt;/th&gt;
          &lt;th&gt;&lt;strong&gt;说明&lt;/strong&gt;&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td style=&quot;text-align: left&quot;&gt;bin&lt;/td&gt;
          &lt;td&gt;该路径下存放了JDK的各种工具命令。&lt;strong&gt;javac&lt;/strong&gt;和&lt;strong&gt;java&lt;/strong&gt;就放在这个目录。&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td style=&quot;text-align: left&quot;&gt;conf&lt;/td&gt;
          &lt;td&gt;该路径下存放了JDK的相关配置文件。&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td style=&quot;text-align: left&quot;&gt;include&lt;/td&gt;
          &lt;td&gt;该路径下存放了一些平台特定的头文件。&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td style=&quot;text-align: left&quot;&gt;jmods&lt;/td&gt;
          &lt;td&gt;该路径下存放了JDK的各种模块。&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td style=&quot;text-align: left&quot;&gt;legal&lt;/td&gt;
          &lt;td&gt;该路径下存放了JDK各模块的授权文档。&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td style=&quot;text-align: left&quot;&gt;lib&lt;/td&gt;
          &lt;td&gt;该路径下存放了JDK工具的一些补充JAR包。&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;配置环境变量&quot;&gt;配置环境变量&lt;/h2&gt;

&lt;p&gt;右击”此电脑”–&amp;gt;”属性”–&amp;gt;”高级系统设置”–&amp;gt;”高级”–&amp;gt;”环境变量”&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/Pyrans/Pyrans.github.io/blob/master/img/2020082902.png?raw=true&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/Pyrans/Pyrans.github.io/blob/master/img/2020082903.png?raw=true&quot; /&gt;&lt;/p&gt;

&lt;p&gt;检测环境变量是否配置成功：在命令提示符窗口输入”javac”,成功输出提示命令表示成功&lt;/p&gt;

&lt;h2 id=&quot;helloworld&quot;&gt;HelloWorld&lt;/h2&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;cm&quot;&gt;/*
编译： 打开命令提示符，输入Javac java文件名+后缀名
运行：输入java 文件名

举例：
javac hello.java
java hello
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Dilank</name></author><category term="Java" /><category term="Java基础" /><summary type="html">JAVA基础环境介绍 JVM JVM是Java Virtual Machine（Java虚拟机）的缩写，JVM是一种用于计算设备的规范，它是一个虚构出来的计算机，是通过在实际的计算机上仿真模拟各种计算机功能来实现的 JRE 是Java程序的运行时环境，包含 JVM 和运行时所需要的核心类库。 我们想要运行一个已有的Java程序，那么只需安装 JRE 即可。 JDK 是Java程序开发工具包，包含 JRE 和开发人员使用的工具。其中的开发工具：编译工具（javac.exe）和运行工具（java.exe）。我们想要开发一个全新的Java程序，那么必须安装 JDK 。 JDK、JRE与JVM的关系 JAVA基础环境安装 1.下载jdk 通过官网下载获取 https://www.oracle.com/ 安装，傻瓜式安装，安装路径中不要包含中文和空格，建议所有的开发工具安装目录统一 jdk的安装目录 目录名称 说明 bin 该路径下存放了JDK的各种工具命令。javac和java就放在这个目录。 conf 该路径下存放了JDK的相关配置文件。 include 该路径下存放了一些平台特定的头文件。 jmods 该路径下存放了JDK的各种模块。 legal 该路径下存放了JDK各模块的授权文档。 lib 该路径下存放了JDK工具的一些补充JAR包。 配置环境变量 右击”此电脑”–&amp;gt;”属性”–&amp;gt;”高级系统设置”–&amp;gt;”高级”–&amp;gt;”环境变量” 检测环境变量是否配置成功：在命令提示符窗口输入”javac”,成功输出提示命令表示成功 HelloWorld public class hello{ public static void main(String[] args){ System.out.println(&quot;Hello World!&quot;) } } /* 编译： 打开命令提示符，输入Javac java文件名+后缀名 运行：输入java 文件名 举例： javac hello.java java hello */</summary></entry><entry><title type="html">Java基础语法</title><link href="https://pyrans.github.io/java/2020/08/28/JAVA%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/" rel="alternate" type="text/html" title="Java基础语法" /><published>2020-08-28T00:00:00+00:00</published><updated>2020-08-28T00:00:00+00:00</updated><id>https://pyrans.github.io/java/2020/08/28/JAVA%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95</id><content type="html" xml:base="https://pyrans.github.io/java/2020/08/28/JAVA%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/">&lt;h2 id=&quot;常量&quot;&gt;常量&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;常量&lt;/strong&gt;：在程序运行过程中，其值不可以发生改变的量。&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;&lt;strong&gt;常量类型&lt;/strong&gt;&lt;/th&gt;
      &lt;th&gt;&lt;strong&gt;说明&lt;/strong&gt;&lt;/th&gt;
      &lt;th&gt;&lt;strong&gt;举例&lt;/strong&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;字符串常量&lt;/td&gt;
      &lt;td&gt;用双引号括起来的内容&lt;/td&gt;
      &lt;td&gt;“HelloWorld”&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;整数常量&lt;/td&gt;
      &lt;td&gt;不带小数的数字&lt;/td&gt;
      &lt;td&gt;666，-88&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;小数常量&lt;/td&gt;
      &lt;td&gt;带小数的数字&lt;/td&gt;
      &lt;td&gt;13.14，-5.21&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;字符常量&lt;/td&gt;
      &lt;td&gt;用单引号括起来的内容&lt;/td&gt;
      &lt;td&gt;‘A’，‘0’， ‘我’&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;布尔常量&lt;/td&gt;
      &lt;td&gt;布尔值，表示真假&lt;/td&gt;
      &lt;td&gt;只有两个值：true，false&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;空常量&lt;/td&gt;
      &lt;td&gt;一个特殊的值，空值&lt;/td&gt;
      &lt;td&gt;值是：null&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;数据类型&quot;&gt;数据类型&lt;/h2&gt;

&lt;h4 id=&quot;存储单元&quot;&gt;存储单元&lt;/h4&gt;

&lt;p&gt;1B（字节） = 8bit&lt;/p&gt;

&lt;p&gt;1KB = 1024B&lt;/p&gt;

&lt;p&gt;1MB = 1024KB&lt;/p&gt;

&lt;p&gt;1GB = 1024MB&lt;/p&gt;

&lt;p&gt;1TB = 1024GB、&lt;/p&gt;

&lt;h4 id=&quot;数据类型-1&quot;&gt;数据类型&lt;/h4&gt;

&lt;p&gt;Java语言是强类型语言，对于每一种数据都给出了明确的数据类型，不同的&lt;strong&gt;数据类型&lt;/strong&gt;也分配了不同的&lt;strong&gt;内存空间&lt;/strong&gt;，所以它们表示的&lt;strong&gt;数据大小&lt;/strong&gt;也是不一样的。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/Pyrans/Pyrans.github.io/blob/master/img/2020082904.png?raw=true&quot; /&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;&lt;strong&gt;数据类型&lt;/strong&gt;&lt;/th&gt;
      &lt;th&gt;&lt;strong&gt;关键字&lt;/strong&gt;&lt;/th&gt;
      &lt;th&gt;&lt;strong&gt;内存占用&lt;/strong&gt;&lt;/th&gt;
      &lt;th&gt;&lt;strong&gt;取值范围&lt;/strong&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;整数&lt;/td&gt;
      &lt;td&gt;byte&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;-128~127&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;short&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;-32768~32767&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;int (默认)&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;-2的31次方到2的31次方-1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;long&lt;/td&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;-2的63次方到2的63次方-1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;浮点数&lt;/td&gt;
      &lt;td&gt;float&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;1.401298e-45到3.402823e+38&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;double (默认)&lt;/td&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;4.9000000e-324  到1.797693e+308&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;字符&lt;/td&gt;
      &lt;td&gt;char&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;0-65535&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;布尔&lt;/td&gt;
      &lt;td&gt;boolean&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;true，false&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;变量&quot;&gt;变量&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;变量：&lt;/strong&gt;在程序运行过程中，其值可以发生改变的量。从本质上讲，变量是内存中一小块区域。&lt;/p&gt;

&lt;p&gt;变量定义：数据类型 变量名 = 变量值;&lt;/p&gt;

&lt;p&gt;举个栗子：int a = 10;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;注意&lt;/strong&gt;：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;变量定义时名字不能重复&lt;/li&gt;
  &lt;li&gt;未赋值的变量不能使用&lt;/li&gt;
  &lt;li&gt;定义long与float类型时，需要在后面加上L或者F,前者防止整数过大，后者防止类型不兼容，举个例子：long a = 10000000000L,  float b = 2.02F;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;标识符&quot;&gt;标识符&lt;/h2&gt;

&lt;p&gt;标识符：即给类、方法、变量等起名字的符号&lt;/p&gt;

&lt;p&gt;标识符定义规则：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;由数字、字母、下划线(_)和美元符($)组成&lt;/li&gt;
  &lt;li&gt;不能以数字开头&lt;/li&gt;
  &lt;li&gt;不能是关键字&lt;/li&gt;
  &lt;li&gt;区分大小写&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;标识符常见命名约定：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;小驼峰命名法：常用于方法、变量命名
    &lt;ul&gt;
      &lt;li&gt;标识符是一个单词的时候，首字母小写&lt;/li&gt;
      &lt;li&gt;标识符由多个单词组成的时候，第一个单词首字母小写，其他单词首字母大写&lt;/li&gt;
      &lt;li&gt;举个栗子：name, myName&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;大驼峰命名法：常用于类命名
    &lt;ul&gt;
      &lt;li&gt;标识符是一个单词的时候，首字母大写&lt;/li&gt;
      &lt;li&gt;标识符由多个单词组成的时候，每个单词的首字母大写&lt;/li&gt;
      &lt;li&gt;举个栗子：Name, MyName&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;类型转换&quot;&gt;类型转换&lt;/h2&gt;

&lt;h4 id=&quot;自动类型转换&quot;&gt;自动类型转换&lt;/h4&gt;

&lt;p&gt;把一个表示数据范围小的数值或者变量赋值给另一个表示数据范围大的变量。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/Pyrans/Pyrans.github.io/blob/master/img/2020082905.png?raw=true&quot; /&gt;&lt;/p&gt;

&lt;p&gt;举个栗子： double a = 10;&lt;/p&gt;

&lt;h4 id=&quot;强制类型转换&quot;&gt;强制类型转换&lt;/h4&gt;

&lt;p&gt;把一个表示数据范围大的数值或者变量赋值给另一个表示数据范围小的变量。&lt;/p&gt;

&lt;p&gt;格式：目标数据类型 变量名 = (目标数据类型)值或者变量;&lt;/p&gt;

&lt;p&gt;举个栗子：int a = (int)88.1;&lt;/p&gt;</content><author><name>Dilank</name></author><category term="Java" /><category term="Java基础" /><summary type="html">常量 常量：在程序运行过程中，其值不可以发生改变的量。 常量类型 说明 举例 字符串常量 用双引号括起来的内容 “HelloWorld” 整数常量 不带小数的数字 666，-88 小数常量 带小数的数字 13.14，-5.21 字符常量 用单引号括起来的内容 ‘A’，‘0’， ‘我’ 布尔常量 布尔值，表示真假 只有两个值：true，false 空常量 一个特殊的值，空值 值是：null 数据类型 存储单元 1B（字节） = 8bit 1KB = 1024B 1MB = 1024KB 1GB = 1024MB 1TB = 1024GB、 数据类型 Java语言是强类型语言，对于每一种数据都给出了明确的数据类型，不同的数据类型也分配了不同的内存空间，所以它们表示的数据大小也是不一样的。 数据类型 关键字 内存占用 取值范围 整数 byte 1 -128~127   short 2 -32768~32767   int (默认) 4 -2的31次方到2的31次方-1   long 8 -2的63次方到2的63次方-1 浮点数 float 4 1.401298e-45到3.402823e+38   double (默认) 8 4.9000000e-324 到1.797693e+308 字符 char 2 0-65535 布尔 boolean 1 true，false 变量 变量：在程序运行过程中，其值可以发生改变的量。从本质上讲，变量是内存中一小块区域。 变量定义：数据类型 变量名 = 变量值; 举个栗子：int a = 10; 注意： 变量定义时名字不能重复 未赋值的变量不能使用 定义long与float类型时，需要在后面加上L或者F,前者防止整数过大，后者防止类型不兼容，举个例子：long a = 10000000000L, float b = 2.02F; 标识符 标识符：即给类、方法、变量等起名字的符号 标识符定义规则： 由数字、字母、下划线(_)和美元符($)组成 不能以数字开头 不能是关键字 区分大小写 标识符常见命名约定： 小驼峰命名法：常用于方法、变量命名 标识符是一个单词的时候，首字母小写 标识符由多个单词组成的时候，第一个单词首字母小写，其他单词首字母大写 举个栗子：name, myName 大驼峰命名法：常用于类命名 标识符是一个单词的时候，首字母大写 标识符由多个单词组成的时候，每个单词的首字母大写 举个栗子：Name, MyName 类型转换 自动类型转换 把一个表示数据范围小的数值或者变量赋值给另一个表示数据范围大的变量。 举个栗子： double a = 10; 强制类型转换 把一个表示数据范围大的数值或者变量赋值给另一个表示数据范围小的变量。 格式：目标数据类型 变量名 = (目标数据类型)值或者变量; 举个栗子：int a = (int)88.1;</summary></entry><entry><title type="html">centos安装virtualenvwrapper</title><link href="https://pyrans.github.io/linux/2020/03/27/centos7%E5%AE%89%E8%A3%85virtualenvwrapper/" rel="alternate" type="text/html" title="centos安装virtualenvwrapper" /><published>2020-03-27T00:00:00+00:00</published><updated>2020-03-27T00:00:00+00:00</updated><id>https://pyrans.github.io/linux/2020/03/27/centos7%E5%AE%89%E8%A3%85virtualenvwrapper</id><content type="html" xml:base="https://pyrans.github.io/linux/2020/03/27/centos7%E5%AE%89%E8%A3%85virtualenvwrapper/">&lt;p&gt;使用pip安装，centos自带的python2.7似乎并不带有pip，这里我们首先安装一个pip:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-linux&quot;&gt;yum install python-pip
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;安装好之后使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip freeze&lt;/code&gt;  可以查看一下状态，如果pip版本过低，或许需要升级一下：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-linux&quot;&gt;pip install --upgrade pip
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;开始安装virtualenvwrapper&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-linux&quot;&gt;pip install virtualenv virtualenvwrapper
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;安装完成设置环境变量:    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vim ~/.bashrc&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-linux&quot;&gt;export WORKON_HOME=.vir                # 这里等于号后面输入的是放置环境变量的目录，可以自己取名字
source /usr/bin/virtualenvwrapper.sh   # 这里输入virtualenvwrapper的路径，一般都在这个目录下面
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;设置完毕，刷新环境变量:    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source ~/.bashrc&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;常用指令：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-linux&quot;&gt;创建虚拟环境：mkvirtualenv [-p 路径] 名字 # []中的字符省略则默认python2.7
激活虚拟环境：workon 环境名称
退出虚拟环境：deactive
列出所有环境：lsvirtualenv -b
进入当前环境目录：cdvirtualenv
复制环境: cpvirtualenv 被复制的环境名称 复制出的环境名称
删除环境：rmvirtualenv 环境名称
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;参考链接：&lt;a href=&quot;https://www.jianshu.com/p/4fecadbc5a48&quot; target=&quot;_blank&quot;&gt;https://www.jianshu.com/p/4fecadbc5a48&lt;/a&gt;&lt;/p&gt;</content><author><name>Pyrans</name></author><category term="linux" /><category term="linux" /><summary type="html">使用pip安装，centos自带的python2.7似乎并不带有pip，这里我们首先安装一个pip: yum install python-pip 安装好之后使用 pip freeze 可以查看一下状态，如果pip版本过低，或许需要升级一下： pip install --upgrade pip 开始安装virtualenvwrapper pip install virtualenv virtualenvwrapper 安装完成设置环境变量: vim ~/.bashrc export WORKON_HOME=.vir # 这里等于号后面输入的是放置环境变量的目录，可以自己取名字 source /usr/bin/virtualenvwrapper.sh # 这里输入virtualenvwrapper的路径，一般都在这个目录下面 设置完毕，刷新环境变量: source ~/.bashrc 常用指令： 创建虚拟环境：mkvirtualenv [-p 路径] 名字 # []中的字符省略则默认python2.7 激活虚拟环境：workon 环境名称 退出虚拟环境：deactive 列出所有环境：lsvirtualenv -b 进入当前环境目录：cdvirtualenv 复制环境: cpvirtualenv 被复制的环境名称 复制出的环境名称 删除环境：rmvirtualenv 环境名称 参考链接：https://www.jianshu.com/p/4fecadbc5a48</summary></entry><entry><title type="html">函数</title><link href="https://pyrans.github.io/python/2018/12/28/%E5%87%BD%E6%95%B0/" rel="alternate" type="text/html" title="函数" /><published>2018-12-28T00:00:00+00:00</published><updated>2018-12-28T00:00:00+00:00</updated><id>https://pyrans.github.io/python/2018/12/28/%E5%87%BD%E6%95%B0</id><content type="html" xml:base="https://pyrans.github.io/python/2018/12/28/%E5%87%BD%E6%95%B0/">&lt;h2 id=&quot;函数&quot;&gt;函数&lt;/h2&gt;

&lt;p&gt;函数的本质：对功能性代码的封装&lt;/p&gt;

&lt;p&gt;函数的优点:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;1、简化了代码结构&lt;/li&gt;
  &lt;li&gt;2、增加了代码的复用度(重复使用程度)&lt;/li&gt;
  &lt;li&gt;3、如果想要修改某些功能或调试某些功能，只需要修改相应的函数即可，不用修改整个项目&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;格式：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def 函数名(参数列表):
    函数体语句
    return 表达式
    
1、def：定义函数的关键字，函数的代码块以def关键字开始
2、函数名：该函数在调用时的名称，命名的规则遵循标识符的命名规则
3、()：参数列表的开始与结束，如果没有参数，小括号不能省略
4、参数列表：格式：(参数1，参数2...., 参数n) 传入函数的参数用逗号隔开，
改参数在函数定义中叫做形参(形式参数)，类似变量名
5、: ：函数体中的内容以冒号开始，四位缩进
6、函数体语句：该函数封装的功能模块
7、return：一般用于函数的结束，将信息返回给函数的调用者
8、表达式：即将返回给函数调用者的信息
注：函数组后 return 表达式可以省略，默认为 return None
注：函数仅定义，未调用时，该函数不会被执行
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;函数的参数&quot;&gt;函数的参数&lt;/h2&gt;

&lt;h4 id=&quot;实参与形参&quot;&gt;实参与形参&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;实参：函数在调用时，传递给函数的形参的数据&lt;/li&gt;
  &lt;li&gt;形参：函数定义时小括号中的变量&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;关键字参数&quot;&gt;关键字参数&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;参数的关键字：在函数调用的传参过程中将形参的名称写在赋值符号之前，将值写在赋值符号之后&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;优点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;1、明确实参对应的形参&lt;/li&gt;
  &lt;li&gt;2、允许函数调用时实参的顺序与函数定义时形参的顺序不一致，但赋值对象不会错乱&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;注：若给函数传参时未使用关键字参数，需按照函数定义时形参的顺序传参&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;举例：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 定义函数
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
&lt;span class=&quot;c1&quot;&gt;# 调用函数
# 普通方式传参
# 按顺序换传参
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'小明'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'18'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'男'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 输出结果: 小明, 18, 男
# 不按顺序传参
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'男'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'小明'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'18'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 输出结果：男, 小明, 18
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 关键字参数方式传参
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'男'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nane&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'小明'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'18'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# 输出结果: 小明, 18, 男
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;参数的默认值&quot;&gt;参数的默认值&lt;/h4&gt;

&lt;p&gt;默认参数: 在函数定义时，直接给形参赋予初始值。&lt;/p&gt;

&lt;p&gt;如果函数在定义时，形参有初始值，函数如果在调用传入了实参，使用传入的数据，如果未传入实参，使用默认的初始值&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;注：如果函数的参数存在默认值，那么将带有默认值的参数放到参数列表的最后面&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;例子:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'小明'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	
&lt;span class=&quot;c1&quot;&gt;# 调用函数
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;# 输出结果: 小明
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'小红'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 输出结果: 小红
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;不定长参数&quot;&gt;不定长参数&lt;/h4&gt;

&lt;p&gt;不定长参数: 能够在函数内部处理比形参个数多的实参&lt;/p&gt;

&lt;h5 id=&quot;args&quot;&gt;*args&lt;/h5&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;加了*的变量，可以存放多个实参，数据类型为元组类型，如果调用时未传入参数，默认为一个空元组，如果传入了实参，将按传入顺序，依次放到元组中
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;举例：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def func1(*args):
	print(args)

# 调用函数:
func1(1, 2, 3, 4)    # 输出结果: (1, 2, 3, 4)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参数包括不定长参数及普通参数:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	
&lt;span class=&quot;c1&quot;&gt;# 调用函数
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# 输出结果: 1, 2, (3, 4, 5)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;注：定义函数时，当不定长参数在前面，函数调用需要使用关键字格式&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fun3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
&lt;span class=&quot;c1&quot;&gt;# 调用函数
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fun4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# 输出结果: 2, (1, 2, 3, 4)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;kwargs&quot;&gt;**kwargs&lt;/h5&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;加了**的变量，可以存放多个实参，数据类型为字典类型，如果调用时未传入参数，默认为一个空字典，如果传入了实参，将按传入顺序，依次放到字典中
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;举例:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
&lt;span class=&quot;c1&quot;&gt;# 调用函数
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'王小明'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'18'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# 输出结果: {'name': '王小明', 'age': '18'}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;函数调用传入参数  key = value&lt;/li&gt;
  &lt;li&gt;当调用时如果全部使用关键字参数形式，会将普通形参按关键字赋值，其他找不到普通形参的关键字，默认放到**kwargs中&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;函数的返回值&quot;&gt;函数的返回值&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;return:将表达式返回给函数的调用者，表达式为什么数据类型，目前函数就可以认为是什么类型，函数的调用者就是什么类型

当函数执行到return时，代表函数执行结束，return后面的代码不再执行
return的作用：1、可以用作函数的返回值
             2、可以用来终止函数
             
             
return可以省略，若省略，则默认为return None
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;匿名函数&quot;&gt;匿名函数&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;匿名函数&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;不使用def关键字定义&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;用lambda关键字来创建&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;语法格式&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;参数1&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;参数2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;参数n&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;表达式&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;表达式&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;只能是一条简单的语句&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;不能包含循环&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;、&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;、&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;yield等&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;允许包含最简&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;单if语句&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;如果表达式为原子类型&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;使用小括号括起来&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;lambda特点&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;、&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lambda只是一个一条语句的表达式&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;整个函数体语句比def简单很多&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;、&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lambda本质为表达式&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;只能封装最简单的逻辑&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;举例:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 定义函数
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 调用函数
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 输出结果: 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;偏函数&quot;&gt;偏函数&lt;/h2&gt;

&lt;p&gt;偏函数： 偏函数用法是指创建一个调用另一个部分——参数或变量已经预置的的函数——的函数的用法。&lt;/p&gt;

&lt;p&gt;举例:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 导入模块functools
# 使用functools.partial  创建偏函数
# 语法格式：
# functiontools.partial(即将使用或更改的原函数名称，原函数各个参数的值)
&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;functiontools&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;myint&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;functiontools&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;partial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# 输出结果: 2
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# 输出结果: 10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Pyrans</name></author><category term="python" /><category term="python" /><summary type="html">函数 函数的本质：对功能性代码的封装 函数的优点: 1、简化了代码结构 2、增加了代码的复用度(重复使用程度) 3、如果想要修改某些功能或调试某些功能，只需要修改相应的函数即可，不用修改整个项目 格式： def 函数名(参数列表): 函数体语句 return 表达式 1、def：定义函数的关键字，函数的代码块以def关键字开始 2、函数名：该函数在调用时的名称，命名的规则遵循标识符的命名规则 3、()：参数列表的开始与结束，如果没有参数，小括号不能省略 4、参数列表：格式：(参数1，参数2...., 参数n) 传入函数的参数用逗号隔开， 改参数在函数定义中叫做形参(形式参数)，类似变量名 5、: ：函数体中的内容以冒号开始，四位缩进 6、函数体语句：该函数封装的功能模块 7、return：一般用于函数的结束，将信息返回给函数的调用者 8、表达式：即将返回给函数调用者的信息 注：函数组后 return 表达式可以省略，默认为 return None 注：函数仅定义，未调用时，该函数不会被执行 函数的参数 实参与形参 实参：函数在调用时，传递给函数的形参的数据 形参：函数定义时小括号中的变量 关键字参数 参数的关键字：在函数调用的传参过程中将形参的名称写在赋值符号之前，将值写在赋值符号之后 优点： 1、明确实参对应的形参 2、允许函数调用时实参的顺序与函数定义时形参的顺序不一致，但赋值对象不会错乱 注：若给函数传参时未使用关键字参数，需按照函数定义时形参的顺序传参 举例： # 定义函数 def test0(name, age, sex): print(name, age, sex) # 调用函数 # 普通方式传参 # 按顺序换传参 test0('小明', '18', '男') # 输出结果: 小明, 18, 男 # 不按顺序传参 test0('男', '小明', '18') # 输出结果：男, 小明, 18 # 关键字参数方式传参 test0(sex='男', nane='小明', age='18') # 输出结果: 小明, 18, 男 参数的默认值 默认参数: 在函数定义时，直接给形参赋予初始值。 如果函数在定义时，形参有初始值，函数如果在调用传入了实参，使用传入的数据，如果未传入实参，使用默认的初始值 注：如果函数的参数存在默认值，那么将带有默认值的参数放到参数列表的最后面 例子: def getName(name='小明'): print(name) # 调用函数 getName() # 输出结果: 小明 getname('小红') # 输出结果: 小红 不定长参数 不定长参数: 能够在函数内部处理比形参个数多的实参 *args 加了*的变量，可以存放多个实参，数据类型为元组类型，如果调用时未传入参数，默认为一个空元组，如果传入了实参，将按传入顺序，依次放到元组中 举例： def func1(*args): print(args) # 调用函数: func1(1, 2, 3, 4) # 输出结果: (1, 2, 3, 4) 参数包括不定长参数及普通参数: def func2(a, b, *args): print(a, b, args) # 调用函数 func2(1, 2, 3, 4, 5) # 输出结果: 1, 2, (3, 4, 5) 注：定义函数时，当不定长参数在前面，函数调用需要使用关键字格式 def fun3(*args, num1): print(num1, args) # 调用函数 fun4(1, 2, 3, 4, num1=2) # 输出结果: 2, (1, 2, 3, 4) **kwargs 加了**的变量，可以存放多个实参，数据类型为字典类型，如果调用时未传入参数，默认为一个空字典，如果传入了实参，将按传入顺序，依次放到字典中 举例: def func1(**kwargs): print(kwargs) # 调用函数 func1(name='王小明', age='18') # 输出结果: {'name': '王小明', 'age': '18'} 注: 函数调用传入参数 key = value 当调用时如果全部使用关键字参数形式，会将普通形参按关键字赋值，其他找不到普通形参的关键字，默认放到**kwargs中 函数的返回值 return:将表达式返回给函数的调用者，表达式为什么数据类型，目前函数就可以认为是什么类型，函数的调用者就是什么类型 当函数执行到return时，代表函数执行结束，return后面的代码不再执行 return的作用：1、可以用作函数的返回值 2、可以用来终止函数 return可以省略，若省略，则默认为return None 匿名函数 匿名函数： 不使用def关键字定义，用lambda关键字来创建 语法格式： lambda 参数1，参数2...参数n：表达式 表达式：只能是一条简单的语句，不能包含循环、return、yield等，允许包含最简 单if语句，如果表达式为原子类型，使用小括号括起来 lambda特点： 1、lambda只是一个一条语句的表达式，整个函数体语句比def简单很多 2、lambda本质为表达式，只能封装最简单的逻辑 举例: # 定义函数 func1 = lambda num1 : num += 1 # 调用函数 print(func1(4)) # 输出结果: 5 偏函数 偏函数： 偏函数用法是指创建一个调用另一个部分——参数或变量已经预置的的函数——的函数的用法。 举例: # 导入模块functools # 使用functools.partial 创建偏函数 # 语法格式： # functiontools.partial(即将使用或更改的原函数名称，原函数各个参数的值) import functiontools myint = functiontools.partial(int, base=2) print(int('2')) # 输出结果: 2 print(myint('2')) # 输出结果: 10</summary></entry><entry><title type="html">字符串相关</title><link href="https://pyrans.github.io/python/2018/12/21/%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9B%B8%E5%85%B3/" rel="alternate" type="text/html" title="字符串相关" /><published>2018-12-21T00:00:00+00:00</published><updated>2018-12-21T00:00:00+00:00</updated><id>https://pyrans.github.io/python/2018/12/21/%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9B%B8%E5%85%B3</id><content type="html" xml:base="https://pyrans.github.io/python/2018/12/21/%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9B%B8%E5%85%B3/">&lt;h2 id=&quot;格式化字符串&quot;&gt;格式化字符串&lt;/h2&gt;

&lt;h4 id=&quot;格式化字符串-1&quot;&gt;%格式化字符串&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;格式化字符串语法格式：
字符串（字符串中包含格式化字符）%（格式化字符对应的数据）
%s ：可以作为字符串、整型、浮点型的占位符
%d ：可以作为整型、浮点型的占位符，如果作为浮点型占位符，默认保留整数部分
%f ：可以作为整型、浮点型的占位符，默认保留六位小数，%f可以写成%。nf格式，n代表保留的小数位数
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用方法:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;print('姓名为%s,年龄为%d,余额为%f'%('王小明', 18, 0.02))
# 输出结果
# 姓名为王小明,年龄为18,余额为0.020000


print('姓名为%(name)s,年龄为%(age)d,余额为%(money)f'%{'name':'小明','age':18,'money':0.02})
# 输出结果
# 姓名为小明,年龄为18,余额为0.020000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;注：当字符串中存在格式化标志时，需要用 %%表示一个百分号&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;format格式化字符串&quot;&gt;format格式化字符串&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;使用位置参数&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;print('my name is {} ,age {}'.format('xiaoming',18))
# 输出结果:
# my name is xiaoming ,age 18


print('my name is {1} ,age {0} {1}'.format(10,'xiaoming'))
# 输出结果
# my name is xiaoming ,age 10 xiaoming


li = ['xiaoming',18]
print('my name is {} ,age {}'.format(*li))
# 输出结果
# my name is xiaoming ,age 18
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;使用关键字参数&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;print('my name is {name},age is {age}'.format(name='xiaoming',age=18))
# 输出结果
# my name is xiaoming,age is 18


di = {'name':'xiaoming','age':18}
print('my name is {name},age is {age}'.format(**hash))
# 输出结果
# my name is xiaoming,age is 18
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;使用索引&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;li = ['xiaoming', 18]
print('name is {0[0]} age is {0[1]}'.format(li))
# 输出结果
# name is xiaoming age is 18
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;转义字符&quot;&gt;转义字符&lt;/h2&gt;

&lt;p&gt;转义字符: 将原本无意义的字符转换为有意义的字符；将原本有意义的字符转为无意义的字符&lt;/p&gt;

&lt;p&gt;r/R: 将所有转义的字符作为普通字符使用，不转义&lt;/p&gt;

&lt;p&gt;\ : 将\后面的字符转义为普通字符&lt;/p&gt;

&lt;p&gt;举个栗子:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;print('\\')
# 报错

print('\\\\')
# 输出结果: \

print(r'\\\\')
# 输出结果: \\\\
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;string内置函数&quot;&gt;string内置函数&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;eval(str): 将str中有效的表达式进行计算并输出结果&lt;/li&gt;
  &lt;li&gt;len(str): 获取字符串长度&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;字母大小写转换&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;str.lower():   将所有字母小写&lt;/li&gt;
      &lt;li&gt;str.upper():   将所有字母大写&lt;/li&gt;
      &lt;li&gt;str.swapcase():  将原字符中的大写转小写，小写转大写&lt;/li&gt;
      &lt;li&gt;str.capitalize():  每个句子首字母大写，其他全小写&lt;/li&gt;
      &lt;li&gt;str.title():  每个单词首字母大写，其他全小写&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;返回指定长度及指定前后符号的字符串&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;str.center(width[,fillchar]):  返回指定长度width的字符串,str在中间,其他位置用fillchar补全,默认fillchar为空格&lt;/li&gt;
      &lt;li&gt;str.ljust(width[,fillchar]):  与str.center()类似,但str在最左边&lt;/li&gt;
      &lt;li&gt;str.rjust(width[,fillchar]):  与str.center()类似,但str在最右边&lt;/li&gt;
      &lt;li&gt;str.zfill(width):  与str.rjust()类似,但其他位置用0补全&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;检测原字符串中是否含有某一个子字符串
    &lt;ul&gt;
      &lt;li&gt;str.find(string[, begin, end]): 检测str中是否存在string，如果存在，返回第一次找到的下标，如果指定begin与end，在该范围内查找；如果找不到，返回-1,从左向右查找&lt;/li&gt;
      &lt;li&gt;str.rfind(string[, begin, end]): 检测str中是否存在string，如果存在，返回第一次找到的下标，如果指定begin与end，在该范围内查找；如果找不到，返回-1,从右向左查找&lt;/li&gt;
      &lt;li&gt;str.index(string[, begin, end]): 与str.find()类似, 但未找到string会报错&lt;/li&gt;
      &lt;li&gt;str.index(string[, begin, end]): 与str.rfind()类似, 但未找到string会报错&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;str.count(string[, begin, end]): 检测str中string出现的次数, 若指定begin与end则在该范围内查找&lt;/li&gt;
  &lt;li&gt;切割字符串
    &lt;ul&gt;
      &lt;li&gt;str.splite(string[, count]): 以string为标志切割str, 返回一个列表，count表示切割次数&lt;/li&gt;
      &lt;li&gt;str.splitelines([boolean]): 按照行切割字符串,若boolean为True,则保留换行符，False则不保留,默认为False&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;连接字符串
    &lt;ul&gt;
      &lt;li&gt;str.join(list):  将list中的元素使用str拼接起来，list中的元素需为字符串&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;max(str), min(str): 返回str中最大、最小的字母(通过比较ASCII码)&lt;/li&gt;
  &lt;li&gt;截掉指定位置的指定元素
    &lt;ul&gt;
      &lt;li&gt;str.lstrip(string)  截掉字符串str左边位置指定的string元素，string默认为空格&lt;/li&gt;
      &lt;li&gt;str.rstrip(string) 截掉字符串str右边位置指定的string元素，string默认为空格&lt;/li&gt;
      &lt;li&gt;str.strip(string)   截掉字符串str两边边位置指定的string元素，string默认为空格&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;替换字符串
    &lt;ul&gt;
      &lt;li&gt;str.replace(old, new[, count]) 将str中的old替换为new,若指定count表示替换count个，默认全部替换&lt;/li&gt;
      &lt;li&gt;str.maketrans(‘old’, ‘new’)   创建映射表替换  &lt;strong&gt;注：替换的字符个数需一致&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;str.maketrans({‘o’: ‘n’, ‘l’: ‘e’, ‘d’: ‘w’})  以键值对方式替换&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;用于判断的函数
    &lt;ul&gt;
      &lt;li&gt;str.isalpha() : 判断字符串中至少有一个字符,且所有字符都为字母&lt;/li&gt;
      &lt;li&gt;str.isalnum() : 判断字符中至少有一个字符，并且所有字符都是数字或字母&lt;/li&gt;
      &lt;li&gt;str.isdigit() : 判断字符中至少有一个字符，并且所有字符都是数字&lt;/li&gt;
      &lt;li&gt;str.isspace() : 判断字符串中至少有一个字符，并且所有字符都是空格&lt;/li&gt;
      &lt;li&gt;str.isupper() : 判断字符串中全为大写字母&lt;/li&gt;
      &lt;li&gt;str.lower() : 判断字符串中全为小写字母&lt;/li&gt;
      &lt;li&gt;str.istitle() : 判断字符串中全为字母，且所有单词首字母大写&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;判断开头与结尾
    &lt;ul&gt;
      &lt;li&gt;str.startwith(string[, start, end]) : 判断str是否以string开头，若指定start与end,则在该范围内判断&lt;/li&gt;
      &lt;li&gt;str.endwith(string[, start, end]) : 判断str是否以string结尾，若指定start与end,则在该范围内判断&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;编码与解码
    &lt;ul&gt;
      &lt;li&gt;编码: str.encode(‘编码格式’)   以指定的编码格式进行编码&lt;/li&gt;
      &lt;li&gt;解码: str.decode(‘编码格式’)    已指定的编码格式进行解码&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;注: python3中默认编码格式为utf-8&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>Pyrans</name></author><category term="python" /><category term="python" /><summary type="html">格式化字符串 %格式化字符串 格式化字符串语法格式： 字符串（字符串中包含格式化字符）%（格式化字符对应的数据） %s ：可以作为字符串、整型、浮点型的占位符 %d ：可以作为整型、浮点型的占位符，如果作为浮点型占位符，默认保留整数部分 %f ：可以作为整型、浮点型的占位符，默认保留六位小数，%f可以写成%。nf格式，n代表保留的小数位数 使用方法: print('姓名为%s,年龄为%d,余额为%f'%('王小明', 18, 0.02)) # 输出结果 # 姓名为王小明,年龄为18,余额为0.020000 print('姓名为%(name)s,年龄为%(age)d,余额为%(money)f'%{'name':'小明','age':18,'money':0.02}) # 输出结果 # 姓名为小明,年龄为18,余额为0.020000 注：当字符串中存在格式化标志时，需要用 %%表示一个百分号 format格式化字符串 使用位置参数 print('my name is {} ,age {}'.format('xiaoming',18)) # 输出结果: # my name is xiaoming ,age 18 print('my name is {1} ,age {0} {1}'.format(10,'xiaoming')) # 输出结果 # my name is xiaoming ,age 10 xiaoming li = ['xiaoming',18] print('my name is {} ,age {}'.format(*li)) # 输出结果 # my name is xiaoming ,age 18 使用关键字参数 print('my name is {name},age is {age}'.format(name='xiaoming',age=18)) # 输出结果 # my name is xiaoming,age is 18 di = {'name':'xiaoming','age':18} print('my name is {name},age is {age}'.format(**hash)) # 输出结果 # my name is xiaoming,age is 18 使用索引 li = ['xiaoming', 18] print('name is {0[0]} age is {0[1]}'.format(li)) # 输出结果 # name is xiaoming age is 18 转义字符 转义字符: 将原本无意义的字符转换为有意义的字符；将原本有意义的字符转为无意义的字符 r/R: 将所有转义的字符作为普通字符使用，不转义 \ : 将\后面的字符转义为普通字符 举个栗子: print('\\') # 报错 print('\\\\') # 输出结果: \ print(r'\\\\') # 输出结果: \\\\ string内置函数 eval(str): 将str中有效的表达式进行计算并输出结果 len(str): 获取字符串长度 字母大小写转换 str.lower(): 将所有字母小写 str.upper(): 将所有字母大写 str.swapcase(): 将原字符中的大写转小写，小写转大写 str.capitalize(): 每个句子首字母大写，其他全小写 str.title(): 每个单词首字母大写，其他全小写 返回指定长度及指定前后符号的字符串 str.center(width[,fillchar]): 返回指定长度width的字符串,str在中间,其他位置用fillchar补全,默认fillchar为空格 str.ljust(width[,fillchar]): 与str.center()类似,但str在最左边 str.rjust(width[,fillchar]): 与str.center()类似,但str在最右边 str.zfill(width): 与str.rjust()类似,但其他位置用0补全 检测原字符串中是否含有某一个子字符串 str.find(string[, begin, end]): 检测str中是否存在string，如果存在，返回第一次找到的下标，如果指定begin与end，在该范围内查找；如果找不到，返回-1,从左向右查找 str.rfind(string[, begin, end]): 检测str中是否存在string，如果存在，返回第一次找到的下标，如果指定begin与end，在该范围内查找；如果找不到，返回-1,从右向左查找 str.index(string[, begin, end]): 与str.find()类似, 但未找到string会报错 str.index(string[, begin, end]): 与str.rfind()类似, 但未找到string会报错 str.count(string[, begin, end]): 检测str中string出现的次数, 若指定begin与end则在该范围内查找 切割字符串 str.splite(string[, count]): 以string为标志切割str, 返回一个列表，count表示切割次数 str.splitelines([boolean]): 按照行切割字符串,若boolean为True,则保留换行符，False则不保留,默认为False 连接字符串 str.join(list): 将list中的元素使用str拼接起来，list中的元素需为字符串 max(str), min(str): 返回str中最大、最小的字母(通过比较ASCII码) 截掉指定位置的指定元素 str.lstrip(string) 截掉字符串str左边位置指定的string元素，string默认为空格 str.rstrip(string) 截掉字符串str右边位置指定的string元素，string默认为空格 str.strip(string) 截掉字符串str两边边位置指定的string元素，string默认为空格 替换字符串 str.replace(old, new[, count]) 将str中的old替换为new,若指定count表示替换count个，默认全部替换 str.maketrans(‘old’, ‘new’) 创建映射表替换 注：替换的字符个数需一致 str.maketrans({‘o’: ‘n’, ‘l’: ‘e’, ‘d’: ‘w’}) 以键值对方式替换 用于判断的函数 str.isalpha() : 判断字符串中至少有一个字符,且所有字符都为字母 str.isalnum() : 判断字符中至少有一个字符，并且所有字符都是数字或字母 str.isdigit() : 判断字符中至少有一个字符，并且所有字符都是数字 str.isspace() : 判断字符串中至少有一个字符，并且所有字符都是空格 str.isupper() : 判断字符串中全为大写字母 str.lower() : 判断字符串中全为小写字母 str.istitle() : 判断字符串中全为字母，且所有单词首字母大写 判断开头与结尾 str.startwith(string[, start, end]) : 判断str是否以string开头，若指定start与end,则在该范围内判断 str.endwith(string[, start, end]) : 判断str是否以string结尾，若指定start与end,则在该范围内判断 编码与解码 编码: str.encode(‘编码格式’) 以指定的编码格式进行编码 解码: str.decode(‘编码格式’) 已指定的编码格式进行解码 注: python3中默认编码格式为utf-8</summary></entry><entry><title type="html">python的运算符</title><link href="https://pyrans.github.io/python/2018/12/20/python%E7%9A%84%E8%BF%90%E7%AE%97%E7%AC%A6/" rel="alternate" type="text/html" title="python的运算符" /><published>2018-12-20T00:00:00+00:00</published><updated>2018-12-20T00:00:00+00:00</updated><id>https://pyrans.github.io/python/2018/12/20/python%E7%9A%84%E8%BF%90%E7%AE%97%E7%AC%A6</id><content type="html" xml:base="https://pyrans.github.io/python/2018/12/20/python%E7%9A%84%E8%BF%90%E7%AE%97%E7%AC%A6/">&lt;h2 id=&quot;算数运算符&quot;&gt;算数运算符&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;加:  +&lt;/li&gt;
  &lt;li&gt;减:  -&lt;/li&gt;
  &lt;li&gt;乘:  *&lt;/li&gt;
  &lt;li&gt;除:  /&lt;/li&gt;
  &lt;li&gt;取余:  %&lt;/li&gt;
  &lt;li&gt;幂:  **&lt;/li&gt;
  &lt;li&gt;整除:  //&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;赋值运算符&quot;&gt;赋值运算符&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;基础赋值运算符: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;组合赋值运算符:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;+=   -=   *=  /=  %=  //=   **= 
a += b  ==&amp;gt; a = a + b
a -= b  ==&amp;gt; a = a - b
a *= b  ==&amp;gt; a = a * b
a /= b  ==&amp;gt; a = a / b
a %= b  ==&amp;gt; a = a % b
a //= b  ==&amp;gt; a = a // b
a **= b  ==&amp;gt; a = a ** b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;比较运算符&quot;&gt;比较运算符&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;等于            ==&lt;/li&gt;
  &lt;li&gt;不等于         !=&lt;/li&gt;
  &lt;li&gt;大于             &amp;gt;&lt;/li&gt;
  &lt;li&gt;小于             &amp;lt;&lt;/li&gt;
  &lt;li&gt;大于等于     &amp;gt;=&lt;/li&gt;
  &lt;li&gt;小于等于     &amp;lt;=&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;逻辑运算符&quot;&gt;逻辑运算符&lt;/h2&gt;

&lt;p&gt;一般返回boolean的值，用于条件判断&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;and     逻辑与&lt;/li&gt;
  &lt;li&gt;or        逻辑或&lt;/li&gt;
  &lt;li&gt;not      逻辑非&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;x and y   x ，y都为True时，整体结论才为True
x or y    x ，y都为False时，整体结论才为False
not(x)    将x的值取反
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;身份运算符&quot;&gt;身份运算符&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;is            : 判断两个对象的内存地址是否一致，一致返回True&lt;/li&gt;
  &lt;li&gt;not is     : 判断两个对象的内存地址是否一致，一致返回False&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;注：is判断内存地址是否相同，而==判断值是否相等&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;举个例子:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a = [1, 2, 3]
b = a
c = [1, 2, 3]
# a与b的内存地址相同，与c的内存地址不同，但abc的值都相同
print(a is b)  # True
print(a == b)  # True
print(a is c)  # False
print(a == c)  # True
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;成员运算符&quot;&gt;成员运算符&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;in            : 某个变量在范围内，返回True&lt;/li&gt;
  &lt;li&gt;not in     : 某个变量不在范围内，返回True&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;举个例子:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;test_list = [1, 2, 3, 4]
print(1 in test_list)      # True
print(1 not in test_list)  # False
print(5 in test_list)      # False
print(5 not in test_list)  # True
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;位运算符&quot;&gt;位运算符&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;amp;  按位与运算符: 参与运算的值的二进制对应位置均为1时，该位置才为1，否则为0
|  按位或运算符: 参与运算的值的二进制对应位置均为0时，该位置才为0，否则为1
~  按位取反运算符: ~a ==&amp;gt; -a-1
^  按位异或运算符: 参与运算的值二进制对应位置有且仅有一个为1，该位置才为1，否则为0
&amp;gt;&amp;gt;  右移运算符   x &amp;gt;&amp;gt; y  将x的二进制数向右移动y个位置  相当于:x/(2**y)
&amp;lt;&amp;lt;  左移运算符   x &amp;lt;&amp;lt; y  将x的二进制数向左移动y个位置  相当于:x*(2**y)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;举个例子:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;print(3 &amp;amp; 4)   3的二进制为11， 4的二进制为100  所以3&amp;amp;4结果为0
print(3 | 4)   结果为7
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;运算符优先级&quot;&gt;运算符优先级&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;运算符&lt;/th&gt;
      &lt;th&gt;描述&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;**&lt;/td&gt;
      &lt;td&gt;指数(&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;~、+、-&lt;/td&gt;
      &lt;td&gt;按位翻转(+-表示正负号)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;* 、/ 、% 、//&lt;/td&gt;
      &lt;td&gt;乘，除，取模和取整除&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;+、-&lt;/td&gt;
      &lt;td&gt;加法减法&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&amp;gt;&amp;gt;、&amp;lt;&amp;lt;&lt;/td&gt;
      &lt;td&gt;右移，左移运算符&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&amp;amp;&lt;/td&gt;
      &lt;td&gt;位与&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;^ |&lt;/td&gt;
      &lt;td&gt;位运算符&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&amp;lt;= 、&amp;gt;=、&amp;lt;、&amp;gt;&lt;/td&gt;
      &lt;td&gt;比较运算符&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;==、!=&lt;/td&gt;
      &lt;td&gt;等于、不等于&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;=、%=、/=、//=、-=、+=、*=、**=&lt;/td&gt;
      &lt;td&gt;赋值运算符&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;is、not is&lt;/td&gt;
      &lt;td&gt;身份运算符&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;in、not in&lt;/td&gt;
      &lt;td&gt;成员运算符&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;not、and、or&lt;/td&gt;
      &lt;td&gt;逻辑运算符&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;注：如果记不住，只要知道小括号的优先级最高即可，大不了嵌套小括号&lt;/strong&gt;&lt;/p&gt;</content><author><name>Pyrans</name></author><category term="python" /><category term="python" /><summary type="html">算数运算符 加: + 减: - 乘: * 除: / 取余: % 幂: ** 整除: // 赋值运算符 基础赋值运算符: = 组合赋值运算符: += -= *= /= %= //= **= a += b ==&amp;gt; a = a + b a -= b ==&amp;gt; a = a - b a *= b ==&amp;gt; a = a * b a /= b ==&amp;gt; a = a / b a %= b ==&amp;gt; a = a % b a //= b ==&amp;gt; a = a // b a **= b ==&amp;gt; a = a ** b 比较运算符 等于 == 不等于 != 大于 &amp;gt; 小于 &amp;lt; 大于等于 &amp;gt;= 小于等于 &amp;lt;= 逻辑运算符 一般返回boolean的值，用于条件判断 and 逻辑与 or 逻辑或 not 逻辑非 x and y x ，y都为True时，整体结论才为True x or y x ，y都为False时，整体结论才为False not(x) 将x的值取反 身份运算符 is : 判断两个对象的内存地址是否一致，一致返回True not is : 判断两个对象的内存地址是否一致，一致返回False 注：is判断内存地址是否相同，而==判断值是否相等 举个例子: a = [1, 2, 3] b = a c = [1, 2, 3] # a与b的内存地址相同，与c的内存地址不同，但abc的值都相同 print(a is b) # True print(a == b) # True print(a is c) # False print(a == c) # True 成员运算符 in : 某个变量在范围内，返回True not in : 某个变量不在范围内，返回True 举个例子: test_list = [1, 2, 3, 4] print(1 in test_list) # True print(1 not in test_list) # False print(5 in test_list) # False print(5 not in test_list) # True 位运算符 &amp;amp; 按位与运算符: 参与运算的值的二进制对应位置均为1时，该位置才为1，否则为0 | 按位或运算符: 参与运算的值的二进制对应位置均为0时，该位置才为0，否则为1 ~ 按位取反运算符: ~a ==&amp;gt; -a-1 ^ 按位异或运算符: 参与运算的值二进制对应位置有且仅有一个为1，该位置才为1，否则为0 &amp;gt;&amp;gt; 右移运算符 x &amp;gt;&amp;gt; y 将x的二进制数向右移动y个位置 相当于:x/(2**y) &amp;lt;&amp;lt; 左移运算符 x &amp;lt;&amp;lt; y 将x的二进制数向左移动y个位置 相当于:x*(2**y) 举个例子: print(3 &amp;amp; 4) 3的二进制为11， 4的二进制为100 所以3&amp;amp;4结果为0 print(3 | 4) 结果为7 运算符优先级 运算符 描述 ** 指数( ~、+、- 按位翻转(+-表示正负号) * 、/ 、% 、// 乘，除，取模和取整除 +、- 加法减法 &amp;gt;&amp;gt;、&amp;lt;&amp;lt; 右移，左移运算符 &amp;amp; 位与 ^ | 位运算符 &amp;lt;= 、&amp;gt;=、&amp;lt;、&amp;gt; 比较运算符 ==、!= 等于、不等于 =、%=、/=、//=、-=、+=、*=、**= 赋值运算符 is、not is 身份运算符 in、not in 成员运算符 not、and、or 逻辑运算符 注：如果记不住，只要知道小括号的优先级最高即可，大不了嵌套小括号</summary></entry><entry><title type="html">MySQL的操作(不包括数据的查询)</title><link href="https://pyrans.github.io/%E6%95%B0%E6%8D%AE%E5%BA%93/2018/12/19/MySQL%E7%9A%84%E6%93%8D%E4%BD%9Cbut%E4%B8%8D%E5%8C%85%E6%8B%AC%E6%95%B0%E6%8D%AE%E7%9A%84%E6%9F%A5%E8%AF%A2/" rel="alternate" type="text/html" title="MySQL的操作(不包括数据的查询)" /><published>2018-12-19T00:00:00+00:00</published><updated>2018-12-19T00:00:00+00:00</updated><id>https://pyrans.github.io/%E6%95%B0%E6%8D%AE%E5%BA%93/2018/12/19/MySQL%E7%9A%84%E6%93%8D%E4%BD%9Cbut%E4%B8%8D%E5%8C%85%E6%8B%AC%E6%95%B0%E6%8D%AE%E7%9A%84%E6%9F%A5%E8%AF%A2</id><content type="html" xml:base="https://pyrans.github.io/%E6%95%B0%E6%8D%AE%E5%BA%93/2018/12/19/MySQL%E7%9A%84%E6%93%8D%E4%BD%9Cbut%E4%B8%8D%E5%8C%85%E6%8B%AC%E6%95%B0%E6%8D%AE%E7%9A%84%E6%9F%A5%E8%AF%A2/">&lt;h2 id=&quot;mysql数据库的操作&quot;&gt;MySQL数据库的操作&lt;/h2&gt;

&lt;h4 id=&quot;创建数据库&quot;&gt;创建数据库&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;语法: create database [if not exists] `数据库名` charset=字符的编码(utf8);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;注：若创建已经存在的数据库会报错，charset后面指定数据库的字符编码&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;​        &lt;strong&gt;数据库名加上反引号(``),则使用关键字作为数据库名可以防止报错，并创建成功&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;查看数据库&quot;&gt;查看数据库&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;显示所有的数据库: show databases;
显示数据库的建表语句: show create database `数据库名`;
查看当前数据库: select database();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;修改数据库&quot;&gt;修改数据库&lt;/h4&gt;

&lt;p&gt;只能修改数据库的字符集&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;alter database `数据库名` charset=字符集选项；
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;删除数据库&quot;&gt;删除数据库&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;drop database [if exists] `数据库名`;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果不使用if exists, 删除一个不存在的数据库会报错&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1008 - Can't drop database '$%@'; database doesn't exist&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;加上 if exists:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;drop database if exists `数据库名`;
#可以避免报错
#作用:判断指定的数据库存不存在,存在则删除.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;选择数据库&quot;&gt;选择数据库&lt;/h4&gt;

&lt;p&gt;选择进行操作的数据库&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;use `数据库名`;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;mysql表的操作&quot;&gt;MySQL表的操作&lt;/h2&gt;

&lt;h4 id=&quot;创建表&quot;&gt;创建表&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;create table [if not exists] `表名`(属性)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;举个栗子:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;create table `table`(
    id int not null auto_increment  primary key comment'主键字段',
    username char(64) comment'用户名' default'root',
    password varchar(64) comment'密码'
    );
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注：最后一句不能加逗号，如果加了表示没写完&lt;/p&gt;

&lt;p&gt;约束条件(列属性)：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1.null|not null   字符是否为空
2. default        默认值
3.auto_increment  自动增长
4.primary key     设为主键
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;给指定的数据库建表:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; create table 数据库名.表名(属性);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注：若使用default设置默认值，默认值不能设置中文，否则乱码&lt;/p&gt;

&lt;h4 id=&quot;查看表&quot;&gt;查看表&lt;/h4&gt;

&lt;p&gt;查看所有的表: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;show tables;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;查看建表结构(建表语句): &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;show create table 表名\G&lt;/code&gt;或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;show create table 表名;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;查看表结构:  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;desc 表名;&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;删除表&quot;&gt;删除表&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;语法:drop table [if exists] `表1`,`表2`;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;修改表&quot;&gt;修改表&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;修改表名&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;语法: alter table `old_name` rename `new_name`;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;增加一个字段&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;alter table `表名` add `字段名` 数据类型;

例子1：
alter table `user` add `age` int(3) first;  #添加在表的第一个

例子2：
alter table `user` add `height` int(3) after `age`; #添加字段在指定字段的后面
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;修改字段属性&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;alter table `表名` modify `属性名(字段名)` 数据类型;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;修改字段名&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;alter table `表名` change `原字段名` `新的字段名` 数据类型;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;修改字段位置&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;alter table `表名` change `字段名` `改为新的字段名` 数据类型 after '字段名';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;修改表的引擎&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;alter table `表名` engine=引擎名;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;移动表到指定数据库并改为指定名称&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;alter table `原表名` rename to 数据库名.表名;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;复制表&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;create table `新表` select * from `原来的表`;

特点:
1.旧表的数据会一起复制过来到新表中
2.不能复制主键
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;create table `新表` like `原来的表`;

特点:
1.它可以复制主键
2.但是不会复制数据
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;mysql数据的操作&quot;&gt;MySQL数据的操作&lt;/h2&gt;

&lt;h4 id=&quot;插入数据&quot;&gt;插入数据&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 插入一条
insert into 表名(字段名) values(值)

插入多条
insert into 表名(字段名) values(值1)，(值2)......
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;插入值的数量应与表名后的字段名相对应&lt;/p&gt;

&lt;p&gt;若插入所有的字段，则表名后的字段名可以省略&lt;/p&gt;

&lt;p&gt;自动增长的字段可以省略，也可设置为0&lt;/p&gt;

&lt;p&gt;md5可以在mysql中直接加密&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;md5(123456) ---&amp;gt;e10adc3949ba59abbe56e057f20f883e
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;修改数据&quot;&gt;修改数据&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;update 表名 set 字段名='值' where 字段(一般使用主键)=值;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;where用于匹配数据&lt;/p&gt;

&lt;h4 id=&quot;删除数据&quot;&gt;删除数据&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 删除一条数据
delete from 表名 where 字段(一般为主键)=值;

# 删除全部数据
delete from 表名 where True;

# 记录原来的建表语句，删除整个表，数据全被清空
truncate 表名;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Pyrans</name></author><category term="数据库" /><category term="mysql" /><summary type="html">MySQL数据库的操作 创建数据库 语法: create database [if not exists] `数据库名` charset=字符的编码(utf8); 注：若创建已经存在的数据库会报错，charset后面指定数据库的字符编码 ​ 数据库名加上反引号(``),则使用关键字作为数据库名可以防止报错，并创建成功 查看数据库 显示所有的数据库: show databases; 显示数据库的建表语句: show create database `数据库名`; 查看当前数据库: select database(); 修改数据库 只能修改数据库的字符集 alter database `数据库名` charset=字符集选项； 删除数据库 drop database [if exists] `数据库名`; 如果不使用if exists, 删除一个不存在的数据库会报错 1008 - Can't drop database '$%@'; database doesn't exist 加上 if exists: drop database if exists `数据库名`; #可以避免报错 #作用:判断指定的数据库存不存在,存在则删除. 选择数据库 选择进行操作的数据库 use `数据库名`; MySQL表的操作 创建表 create table [if not exists] `表名`(属性) 举个栗子: create table `table`( id int not null auto_increment primary key comment'主键字段', username char(64) comment'用户名' default'root', password varchar(64) comment'密码' ); 注：最后一句不能加逗号，如果加了表示没写完 约束条件(列属性)： 1.null|not null 字符是否为空 2. default 默认值 3.auto_increment 自动增长 4.primary key 设为主键 给指定的数据库建表: create table 数据库名.表名(属性); 注：若使用default设置默认值，默认值不能设置中文，否则乱码 查看表 查看所有的表: show tables; 查看建表结构(建表语句): show create table 表名\G或者show create table 表名; 查看表结构: desc 表名; 删除表 语法:drop table [if exists] `表1`,`表2`; 修改表 修改表名 语法: alter table `old_name` rename `new_name`; 增加一个字段 alter table `表名` add `字段名` 数据类型; 例子1： alter table `user` add `age` int(3) first; #添加在表的第一个 例子2： alter table `user` add `height` int(3) after `age`; #添加字段在指定字段的后面 修改字段属性 alter table `表名` modify `属性名(字段名)` 数据类型; 修改字段名 alter table `表名` change `原字段名` `新的字段名` 数据类型; 修改字段位置 alter table `表名` change `字段名` `改为新的字段名` 数据类型 after '字段名'; 修改表的引擎 alter table `表名` engine=引擎名; 移动表到指定数据库并改为指定名称 alter table `原表名` rename to 数据库名.表名; 复制表 create table `新表` select * from `原来的表`; 特点: 1.旧表的数据会一起复制过来到新表中 2.不能复制主键 create table `新表` like `原来的表`; 特点: 1.它可以复制主键 2.但是不会复制数据 MySQL数据的操作 插入数据 # 插入一条 insert into 表名(字段名) values(值) 插入多条 insert into 表名(字段名) values(值1)，(值2)...... 插入值的数量应与表名后的字段名相对应 若插入所有的字段，则表名后的字段名可以省略 自动增长的字段可以省略，也可设置为0 md5可以在mysql中直接加密 md5(123456) ---&amp;gt;e10adc3949ba59abbe56e057f20f883e 修改数据 update 表名 set 字段名='值' where 字段(一般使用主键)=值; where用于匹配数据 删除数据 # 删除一条数据 delete from 表名 where 字段(一般为主键)=值; # 删除全部数据 delete from 表名 where True; # 记录原来的建表语句，删除整个表，数据全被清空 truncate 表名;</summary></entry></feed>