你知道怎么从Python角度学习Java基础

2022-02-17

目录
  • 1. 变量
    • 赋值
    • 数据类型
  • 2. 符号
    • 计算运算符
    • 比较运算符
    • 代码符
    • 注释
    • 文本符
  • 3. if
    • 一行if
    • 一次判断
    • 多次判断
  • 4. for
    • 5. while
      • 6. 数组
        • 7. 程序结构
          • 8. 输入输出
            • 9. 异常捕获
              • 总结

                1. 变量

                赋值

                项目 Java Python JavaScript VBA
                必须先声明
                声明 int x; dim x%
                赋值 x=1; x=1 x=1 x=1
                声明并赋值 int x=1; x=1 x=1
                null None null undefined Null

                数据类型

                项目 Java Python JavaScript VBA
                整数 int x=1; x=1 x=1 x=1
                字符 char a='A';
                字符串 String a="A"; a="A"
                a='A'
                a="A"
                a='A'
                a="A"
                小数 float f=3.14f;
                double d=1.7d
                f=3.14 f=3.14 f=3.14
                布尔 boolean b=true; b=True b=true b=True
                常量 final double PI=3.14; PI=3.14 const PI=3.14 Const PI=3.14
                对象 StringBuilder sb = new StringBuilder();
                var sb = new StringBuilder();
                sb = ShaBi() sb = new Shabi() x = CreateObject("Scripting.Dictionary")
                类型转换 只允许向上转换 允许 允许 允许

                2. 符号

                计算运算符

                运算符 Java Python JavaScript VBA
                + + + +
                - - - -
                * * * *
                / / / /
                求余 % % % mod
                次幂 3**2 3**2
                自增 ++ ++
                自减 -- --
                叠加 += += +=
                叠减 -= -= -=
                叠乘 *= *= *=
                叠除 /= /= /=
                括号 () () () ()
                字符串连接 + + + +

                比较运算符

                运算符 Java Python JavaScript VBA
                大于 > > > >
                大于等于 >= >= >= >=
                小于 < < < <
                小于等于 <= <= <= <=
                等于 == == == ==
                不等于 != != != !=
                and && and && and
                or || or || or
                not ! not ! not

                代码符

                符号 Java Python JavaScript VBA
                转义符 \ \ \ “”
                换行符 ; : ; :
                换行符是否可省略 不可省略 大部分可省略 大部分可省略

                注释

                符号 Java Python JavaScript VBA
                单行注释 // # //
                多行注释 /*…*/ “”"…"""
                ’’’…’’’
                /*…*/

                文本符

                符号 Java Python JavaScript VBA
                单行字符 "
                "
                "
                单行字符串 " "
                "
                "
                多行字符串 “”"…""" “”"…"""
                ’’’…’’’

                3. if

                一行if

                // Javax = a > b ? c : d;
                # Python
                x = c if a > b else d
                
                // JavaScript
                x = a > b ? c : d
                
                ' VBA
                if a > b Then x = c Else x = d
                

                一次判断

                // Java
                if (a > b) {
                	x = c;
                } else {
                	x = d;
                }
                
                # Python
                if a > b:
                	x = c
                else:
                	x = d
                
                // JavaScript
                if (a > b) {
                	x = c
                } else {
                	x = d
                }
                
                ' VBA
                If a > b Then
                	x = c
                Else
                	x = d
                End If
                

                多次判断

                // Java
                if (a > b) {
                	x = c;
                } else if (a > bb) {
                	x = cc;
                } else {
                	x = d;
                }
                
                # Python
                if a > b:
                	x = c
                elif a > bb:
                	x = cc
                else:
                	x = d
                
                // JavaScript
                if (a > b) {
                	x = c
                } else if (a > bb) {
                	x = cc
                } else {
                	x = d
                }
                
                ' VBA
                If a > b Then
                	x = c
                ElseIf a > bb Then
                	x = cc
                Else
                	x = d
                End If
                

                4. for

                下标循环

                // Java
                for (int i=0;i<100;i++) {	
                	System.out.println(i);
                }
                
                # Python
                for i in range(100):
                	print(i)
                
                // JavaScript
                for (var i=0;i<100;i++) {	
                	console.log(i)
                }
                
                ' VBA
                For i = 1 to 100 step 1
                	Debug.Print i
                next
                

                数组遍历循环

                // Java
                for (int a:arr) {
                	System.out.print(a);
                }
                
                # Python
                for a in arr:
                	print(a)
                
                // JavaScript
                for (a in arr) {
                	console.log(a)
                }
                
                ' VBA 
                For Each a in arr
                	Debug.Print a
                Next
                
                项目 Java Python JavaScript VBA
                中断循环 break break break Exit For
                跳过循环 continue continue continue goto

                5. while

                // Java
                int i;
                while (i < 100) {
                	System.out.println(i);
                	i++;
                }
                
                // java的另一个while
                int i;
                do {
                	System.out.println(i);
                	i++;
                } while (i < 99);
                
                # Python
                i = 0
                while True:
                	if i < 100:
                		print(i)
                	else:
                		break
                
                // JavaScript
                i = 0
                while (i < 100) {
                	console.log(i)
                	i++
                }
                
                ' VBA
                ' 1
                i = 0;
                While i < 100
                	Debug.Print(i)
                Wend
                
                ' VBA
                ' 2
                i = 0;
                Do While i < 100
                	Debug.Print(i)
                Loop
                
                ' VBA
                ' 3
                i = 0;
                Do 
                	Debug.Print(i)
                Loop While i < 99
                
                ' VBA
                ' 4
                i = 0;
                Do Until i >= 100
                	Debug.Print(i)
                Loop
                
                ' VBA
                ' 5
                i = 0;
                Do
                	Debug.Print(i)
                Loop Until i >= 99
                
                项目 Java Python JavaScript VBA
                中断循环 break break break Exit For
                跳过循环 continue continue continue goto

                6. 数组

                项目 Java Python JavaScript VBA
                定义 int[] x = {1,2,3,4,5}; x = [1,2,3,4,5] x = [1,2,3,4,5] dim Arr()
                符号 {} []
                {}
                ()
                [] Array()
                索引 x[0]; x[0] x[0] Arr(0)
                类型混用 不允许 x=[1,'a'] x=[1,'a'] Arr=Array(1,"a")
                不允许 x.append('b')
                x.insert(0,'c')
                x.push('b') Redim Preserve Arr(4)
                Arr(4) = 3
                不允许 x.pop(1)
                del x[1]
                x.pop(1) Redim Arr(1)
                x[0] = 6; x[0] = 6 x[0] = 6 Arr(0)=6

                7. 程序结构

                Java

                /**
                * 文档注释
                */
                public class Hello {
                	public static void main(String[] args) {
                		// 主程序说明
                		userFunction usf = new userFunction();
                		usf.setArg("Hello"); 
                		System.out.println(usf.getArg());
                		/* 多行注释
                		分行 */
                	}
                }	
                
                class userFunction {
                	private String arg;
                	
                	public void setArg(String arg) {
                		// 设置
                		this.arg = arg;
                	}
                	
                	public String getArg() {
                		// 返回
                		return this.arg;
                	}	
                }
                

                Python

                '''
                文档说明
                '''
                
                class userFunction:
                	def __init__(self):
                		pass
                		
                	def setArg(self,arg):
                		self.arg = arg
                	
                	def getArg(self):
                		return self.arg
                
                if __name__ == '__main__':
                	usf = userFunction()
                	usf.setArg("Hello")
                	print(usf.getArg())
                

                JavaScript

                function userFunction(args) {
                	x = process(args)
                	return x
                }
                

                VBA

                Sub userSub()
                	x = userFunction(args)
                	Debug.Print x
                End Sub
                
                Function userFunction(args) as String
                	userFunction = process(args)
                End Function
                

                8. 输入输出

                输出

                项目 Java Python JavaScript VBA
                输出 System.out.println
                System.out.print
                print console.log Debug.Print
                格式化输出 System.out.printf
                System.out.format
                format
                快速格式化 f'{d} is a number' `${d} is a number`

                输入

                项目 Java Python JavaScript VBA
                输入 import java.util.Scanner

                Scanner scanner = new Scanner(System.int);
                String ipt = scanner.nextLine();
                ipt = input('请输入:') var ipt = prompt('请输入','预设值') ipt = InputBox("请输入",,"预设值")

                9. 异常捕获

                项目 Java Python JavaScript VBA
                异常捕获 try {..}
                catch {...}
                finally {...}
                try:
                except:
                finally:
                try {..}
                catch {...}
                finally {...}
                On error goto tag

                总结

                本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注北冥有鱼的更多内容!          

                《你知道怎么从Python角度学习Java基础.doc》

                下载本文的Word格式文档,以方便收藏与打印。