Java学习
第一天下午:Java基础语法
1、输出
package com.company;
public class Main {
public static void main(String[] args) {
System.out.println("Hello Zhu");
}
}
2、标识符
Java 所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。 关于 Java 标识符,有以下几点需要注意: 所有的标识符都应该以字母(A-Z 或者 a-z),美元符、或者下划线开始 首字符之后可以是字母(A-Z 或者 a-z),美元符、下划线或数字的任何字符组合 关键字不能用作标识符 标识符是大小写敏感的 合法标识符举例:age、$salary、_value、__1_value 非法标识符举例:123abc、-salary
package com.company;
public class Main {
public static void main(String[] args) {
int x = 10;
}
}
3、常量 变量
不会变的10、30【常量】 会变的string a【变量】
package com.company;
public class Main {
public static void main(String[] args) {
String name = "朱帅";
System.out.println(name);
String name2 = "帅帅";
System.out.println(name2);
}
}
4、显示一个界面
package com.company;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setVisible(true);
}
}
package com.company;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setSize(300, 300);
jFrame.setLocation(300, 700);
jFrame.setTitle("🐽");
jFrame.setVisible(true);
}
}
5、显示一个弹窗
package com.company;
import jdk.nashorn.internal.scripts.JO;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "这是一个弹窗");
}
}
6、运算符
+、-、*、/ ++、– &&、||、!
package com.company;
import jdk.nashorn.internal.scripts.JO;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
int x = 3 + 5;
System.out.println(x);
int y = 3 * 5;
System.out.println(y);
int z = 3 / 5;
System.out.println(z);
}
}
package com.company;
import jdk.nashorn.internal.scripts.JO;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
System.out.println(3 == 3);
int w = 10;
int j = 20;
System.out.println(w > j);
}
}
7、流程控制语句
–if –if else
8、循环??
–while
9、小作业
- 运用目前学习过知识,将数值123.5678四舍五入保留两位小数
package com.company;
import jdk.nashorn.internal.scripts.JO;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
double num;
int ans;
num = 123.5678;
num = num * 100;
num = (double) (int)((num + 0.5)) / 100;
ans = (int)(num + 0.5);
System.out.println(ans);
}
}
- 使用循环语句完成以下要求:
计算数字1-100所有数字的和 计算1-100所有偶数的和
package com.company;
import jdk.nashorn.internal.scripts.JO;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 100; i ++){
sum = sum + i;
}
System.out.println(sum);
int sums = 0;
for(int i = 1; i <= 100; i ++){
if(i % 2 == 0)
sums = sums + i;
}
System.out.println(sums);
}
}
package com.company;
import jdk.nashorn.internal.scripts.JO;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 9; i ++){
for(int j = 1; j <= i; j ++){
System.out.print(i + "*" + j +"="+ i * j + " ");
}
System.out.println();
}
}
}
- 编程输出所有的三位水仙花数 水仙花数:各位数字的立方数相加等于该数本身 例如 153 111+555+333=153 153就是一个三位水仙花数
package com.company;
import jdk.nashorn.internal.scripts.JO;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
int a, b, c;
for(int i = 100; i <= 999; i ++){
a = i / 100;
b = (i % 100) / 10;
c = i % 10;
if(a * a * a + b * b * b + c * c * c == i)
System.out.println(i);
}
}
}
- 计算一个整数数组{89,95,68,76,88,64}的平均值
package com.company;
import jdk.nashorn.internal.scripts.JO;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
int num = 0;
int a[] = {89, 95, 68, 76, 88, 64};
for(int i = 0; i < 6; i ++){
num = num + a[i];
}
num = num / 6;
System.out.println(num);
}
}
Java基础语言
第二天上午
1、Java中类的定义
类:是模具【功能、属性】 功能一样:行为一样 属性不一样:颜色,内存大小
在IDEA中创建一个Student类
2、成员变量的定义
3、成员方法的声明
package com.company;
public class Student {
String name;
int age;
String sex;
public void study(){
System.out.println("我在学习");
}
public void eat(){
System.out.println("干饭");
}
}
4、类的使用
新建一个StudentTest类
package com.company;
public class StudentTest {
public static void main(String[] args){
Student stu = new Student();
Student stu1 = new Student("帅", "女", 3);
stu.eat();
stu1.eat();
stu.study();
stu1.study();
stu.name = "laozhu";
stu.sex = "女";
System.out.println(stu.name);
System.out.println(stu1.name);
}
}
5、方法的重载
6、构造方法的使用
package com.company;
public class Student {
public Student(){
System.out.println("🐽构造方法执行了");
}
public Student(String name, String sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
System.out.println("有参数的构造方法执行了,给属性赋值🐽");
}
String name;
int age;
String sex;
public void study(){
System.out.println("我在学习");
}
public void eat(){
System.out.println("干饭");
}
}
package com.company;
public class StudentTest {
public static void main(String[] args){
Student stu = new Student();
Student stu1 = new Student("帅", "女", 3);
7、构造方法的使用场景
package com.company;
public class Student {
String name;
int age;
String sex;
public Student(){
System.out.println("🐽构造方法执行了");
}
public Student(String name, String sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
System.out.println("有参数的构造方法执行了,给属性赋值🐽");
}
}
8、this关键的使用
9、实例成员与类成员
1. 定义
2. 实例变量和类变量的区别
3. 类方法和实例方法的区别
4. 实例成员和类成员
5. 访问修饰词
10、封装的使用
点击进行getter、setter的添加进行封装 Person类
package com.company;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
if(this.age > 150)
this.age = 150;
if(this.age < 1)
this.age = 1;
}
}
PersonTest
package com.company;
public class PersonTest {
public static void main(String[] args){
Person per = new Person();
per.setName("老朱");
per.setAge(40);
}
}
11、继承的使用
1. JAVA继承语法
父类
package com.company;
public class Animal {
String name;
String car = "宝马";
public void eat(){
System.out.println("动物吃");
}
}
子类
package com.company;
public class Dog extends Animal{
}
2. 子类的扩展
子类
package com.company;
public class Dog extends Animal{
String color;
public void shout(){
System.out.println("旺旺,旺旺旺,...");
}
}
3. 继承的使用
每一个类都只有一个父类,而最终的👨类是Object AnimalTest文件
package com.company;
public class AnimalTest {
public static void main(String[] args) {
Cat cat = new Cat();
int x = cat.hashCode();
System.out.println(x);
}
}
Cat类
package com.company;
public class Cat {
}
运行结果
4. 方法的重写
Animal类
package com.company;
public class Animal {
String name;
String car = "宝马";
public void eat(){
System.out.println("动物吃");
}
}
Dog类
package com.company;
public class Dog extends Animal{
String name;
String color;
String car = "奔驰🚗";
public void shout(){
System.out.println("旺旺,旺旺旺,...");
}
public void eat(){
System.out.println("小狗吃🌧?");
}
public void say(){
System.out.println("我的车" + super.car);
System.out.println("我的车" + this.car);
}
}
AnimalTest文件
package com.company;
public class AnimalTest {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "gou";
dog.color = "绿";
dog.eat();
dog.say();
}
}
5. super关键字
Dog类
package com.company;
public class Dog extends Animal{
public void say(){
System.out.println("我的车" + super.car);
System.out.println("我的车" + this.car);
}
}
第二天下午
1、接口(没太明白这个部分)
接口的优先级大于继承 一个接口可以继承多个接口 创建一个USB的interface
package day02;
public interface USB {
int a = 10;
public void on();
}
创建一个printer的类
package day02;
public class Printer implements USB{
public void on() {
System.out.println("🖨?开始启动");
}
}
创建一个Mouse的类
package day02;
public class Mouse implements USB{
public void on(){
System.out.println("🖱?开始启动");
}
}
创建一个KeyBoard的类
package day02;
public class KeyBoard implements USB{
public void on(){
System.out.println("开始??启动");
}
}
在Cu类里面使用
package day02;
public class Cu {
public void getSheBei(USB usb){
usb.on();
}
public static void main(String[] args){
Mouse mouse = new Mouse();
Cu c = new Cu();
c.getSheBei(mouse);
}
}
|