JAVA
学习
第一个程序
public class Main
{
public static void main(String[] args)
{
System.out.println("Hi World");
}
}
项目结构
public class Main
{
public static void main(String[] args)
{
System.out.println("Java");
System.out.println("is");
System.out.println("cool");
// this is a comment
//TODO
}
}
数据类型
public class Main
{
public static void main(String[] args)
{
System.out.println("hello");
System.out.println(10);
System.out.println(20);
System.out.println(123456789123456L);
System.out.println(3.1415);
System.out.println(3.1415F);
System.out.println(true);
System.out.println(false);
}
}
长整型用于表示大数字,在后面添加字母“L”(单词“long”的缩写)来区分。整型占用32位二进制,能表示的最大数值为2147483647;长整型则占用64位。
在Java中,标准的浮点数类型叫做“double”,精度较高,另外还有一种低精度浮点数“float”。数值后面的字母“F”表示这是一个“float”型浮点数,而不是“double”型。
变量
public class Main
{
public static void main(String[] args)
{
int i = 500;
System.out.println(i);
int j = 10;
System.out.println(j);
int k = i + j;
System.out.println(k);
int x = i * j;
System.out.println(x);
}
}
public class Main
{
public static void main(String[] args)
{
int i = 10;
System.out.println(i);
++i;
System.out.println(i);
--i;
System.out.println(i);
}
}
变量类型
public class Main
{
public static void main(String[] args)
{
int i = 10;
System.out.println(i);
double x = 1.5;
System.out.println(x);
double pi = 3.1415;
System.out.println(pi);
String s = "Hello";
System.out.println(s);
long v = 123456789123456L;
System.out.println(v);
boolean b = true;
System.out.println(b);
}
}
控制流语句
if条件语句
public class Main
{
public static void main(String[] args)
{
int i = 100;
if (i < 10)
{
System.out.println("i is less than 10");
}
if (i > 0)
{
System.out.println("i is greater than 0");
}
if (i%2 == 0)
{
System.out.println("i is even");
}
}
}
if-else语句
public class Main
{
public static void main(String[] args)
{
int a = 100;
int b = 1000;
int min;
if (a < b)
{
min = a;
}
else
{
min = b;
}
System.out.println("The minimum of a and b is: " + min);
}
}
while循环语句
public class Main
{
public static void main(String[] args)
{
int sum = 0;
int num = 1;
// Use a while loop to calculate the sum of 1 to 1000
while ( num <= 1000)
{
sum = sum +num;
num++;
}
System.out.println(sum);
}
}
for循环语句
public class Main
{
public static void main(String[] args)
{
// for (initialization; condition; increment)
for (int i = 10; i >= 1; i--)
{
System.out.println(i);
}
}
}
静态方法
public class Main
{
public static void main(String[] args)
{
System.out.println(mult(10, 20));
System.out.println(mult(100, 200));
}
static int mult(int x, int y)
{
int z = x * y;
return z;
}
}
数组
public class Main
{
public static void main(String[] args)
{
int[] b = {2,4,6,7};//建立新数组
for (int i = 0; i < b.length; i++) //for循环输出所有值
{
System.out.println(b[i]);
}
int[] a = new int[4];//建立新空数组,长度为4值均为0
a[0] = 5;
System.out.println(a[0]);
a[1]=7;
System.out.println(a[1]);
}
}
类和对象
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
rect.width = 100;
System.out.println(rect.width);
rect.height = 200;
System.out.println(rect.height);
Rectangle rect2 = new Rectangle();
rect2.width = 10;
System.out.println(rect2.width);
rect2.height = 20;
System.out.println(rect2.height);
Point p = new Point();
p.x = 4;
p.y = 5;
System.out.println(p.x);
System.out.println(p.y);
rect2.position = p;
System.out.println(rect2.position.x);
System.out.println(rect2.position.y);
}
}
class Point
{
int x;
int y;
}
class Rectangle
{
int width;
int height;
Point position;
}
构造器
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.width);
System.out.println(rect.height);
Rectangle rect2 = new Rectangle(10,20);
System.out.println(rect2.width);
System.out.println(rect2.height);
Rectangle rect3 = new Rectangle();
System.out.println(rect3.width);
System.out.println(rect3.height);
}
}
class Rectangle
{
int width;
int height;
// A constructor
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
Rectangle()
{
this.width = 100;
this.height = 100;
}
}
感想:Rectangle()构造器类似于python中方法的默认值,当传入数据(即构造器Rectangle(int width, int height)),优先数据,未传入时就启用默认。
方法
一个对象包含三种最常见的成员:构造器(参考上一课)、数据和方法。数据用于定义状态,而方法是行为特征的抽象。
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(100, 200);
System.out.println(rect.getWidth());
System.out.println(rect.getHeight());
System.out.println(rect.getArea());
rect.setWidth(10);
rect.setHeight(20);
System.out.println(rect.getArea());
rect.scale(10);
System.out.println(rect.getArea());
}
}
class Rectangle
{
int width;
int height;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
void setHeight(int height)
{
this.height = height;
}
void scale(int factor)
//等比缩放
{
this.width = this.width * factor;
this.height = this.height * factor;
}
void setWidth(int width)
{
this.width = width;
}
int getWidth()
{
return this.width;
}
int getHeight()
{
return this.height;
}
int getArea()
{
return this.width * this.height;
}
}
eg.设定矩形并获取边长面积以及等比缩放