第一课

一.基础方法

1 注释

1.什么是注释?

对程序中的代码进行解释说明

2.注释的分类

分为多行注释,单行注释和文本注释(文本注释放在功能前面)

/**
文本注释
**/
public class Demo{
public static void main(String[] args) {
//单行注释
System.out.println("==============")
/*
 *多行注释
*/ 

2.字面量

1.什么是字面量?

数据在程序中的书写格式

2.字面量的分类

整数,小数,布尔值,字符,空值(null),特殊字符

public class Demo {
public static void main(String[] args) {
//1.整数字面量,直接写
System.out.println(1);
//2.浮点型字面量,直接写(小数) 
System.out.println(1.0);
//3.布尔型字面量,只有true false
 System.out.println(true);
 System.out.println(false);
//4.字符型,用单引号括起来,里面有且仅有一个字符 
System.out.println('a');
//System.out.println('ab');报错
//特殊字符:\n(换行功能)\t(缩进功能)
System.out.println("Hello \n World"); 
System.out.println("Hello \t World");
//5.字符串变量,用双引号,可以是任意字符 
System.out.println("aaa");
3.书写的格式的注意

字符用单引号有且仅有一个字符,字符串用双引号

3.变量

一.认识什么是变量

1.什么是变量?

用来记住程序中要处理的数据

2.变量类型

数据类型 变量名称 = 初始值

3特点

可以被替换

class d {
blic static void main(String[] args) {
 printVariable();
blic static void printVariable(){ 
//定义变量:数据类型变量名称=初始值 
double score=90;
System.out.println(score);
 System.out.println("==

//特点:可以被替换 
int age=18; 
age=19;
System.out.println(age);
 age=age+1;
System.out.println(age);
  }
}

二.数据类型

1.基本数据类型

整形:byte(1),short(2),int(4),long(8)

浮点型(小数):float(4),double(8)

字符型:char(2)

布尔型:boolean(1)

public class d2 {
public static void main(String[] args) {
printhello();}
public static void printhello(){	法	
//1.整型 byte b=10;
//byte b1=128;报错越界了 
short s=10; int i=10; long l=10;
//注意:随便写一个整数是默认为int类型,超过了int类型的取值范围,
// 如果想要实现long类型,就在后面加L
// long l1=344444444444444444444;
//2.浮点型
//随便写一个浮点数子是默认为double类型,希望是float类型必须加上f或者F float f=10;
 double d=10;
//3.字符型 
char c=10;
//4.布尔型
boolean b1=true;
//5.字符串
String str="hello";
System.out.println(str);
  }
}
2.整数和小数字面量默认为什么?

默认为int,加上f或者F就是数据了

4.关键字,标识符

1.什么是关键字和标识符?

关键字是定义数据类型,不能用来做类名,变量名

标识符是程序自定义的名字,有数字,字母,下划线,美元符组成,不能用数字开头和特殊符号

二.基本语法

1.方法

1方法是什么?(定义)

定义:用于执行特定任务或操作的代码块

接收数据并处理,在返回一个处理值结果

2.格式是什么?

修饰符 返回值类型 方法名(形参列表)

方法体代码(需要执行的功能代码)

return(返回值)

public class d{
public static void main(String[] args) {
int sum=getSum( a: 10,b:20);
System.out.println("和是:"+sum);

System.out.println("=========");

int sum2=getSum( a: 103,b: 202); 
System.out.println("和是:"+sum2);
//定义一个方法,求任意两个整数的和并返回
public static int getsum(int a,int b){
return a+bi
  }
}
3.考虑哪两个方面以及如何只用

一个是是否需要接受数据和接收数据是否返回数据。

必须调用后才可以使用;调用格式:方法名称(...).

不返回值时要带void

2方法重载

1.什么是方法重载?

一个类中多个方法名称相同,但是形参列表不同

注意:只关心方法名称相同,形参列表不同(类型不同,个数不同,顺序不同)。无返回值可以直接用return直接结束

public class D2 {
public static void main(String[] args){
getSum( a:2,b: 3);
public static void getSum(int a,double b){
System.out.println(a);

3.类型转换

1.表达式的自动类型转换是什么样的?

小范围会自动转换为大范围

2.最终由谁决定?

有表达试中最高的类型决定

3.需要注意什么?

Byte short char 直接转换成int类型参与运算

public static double calclint a, double b,int c,char r
//表达式的最终结果是由最高类型决定的
return a+b+c+r;

public static int calc2(byte a,byte b){ 1个用法
//byte short char运算时会直接int运算 return a+bi}
public static byte calc3(byte a,byte b){ 1个用法
byte c=(byte)(a+b); return ci}

4.输入输出

1.什么是输入和输出?

输入是system.out.println("====")

输出是Scanner

package com.jinbenyufa.fangfa1;
//导包,告诉我们的程序去JDk找

Scannerimport java.util.Scanner;

public class D4 {
public static void main(String[] args){
printUserInfo();
}
public static void printUserInfo(){ 
Scanner sc =new Scanner(System.in);

System.out.println("请输入用户名:");
//让程序暂停,等用户输入字符串
String username =sc.nextLine();
System.out.println("你叫"+username);
System.out.println("输入年龄");
int age = sc.nextInt();
System.out.println("年龄"+age);
//打印
  }
}

5.运算符

1.基本运算符

+ - * / %

public class D5 {
public static void main(String[] args){
print( a:20,b: 4);
}
public static void print(int a,int b){ 1个用法
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println((double)a/b);
System.out.println(1.0*a/b);
System.out.println(a%b);
  }
}

"+"可以做连接符

2.自增自减

符号:++(自增) --(自减)

public class D5 {
public static void main(String[] args){
print( a: 20,b: 4);
System.out.println("============"")
print2( a: 10);
public static void print(int a,int b){...}
public static void print2(int a){ 1个用法
a++;
++a;
System.out.println(a);
  }
}

3.赋值运算符

赋值符号:"="

public class D5 {
public static void main(string[] args){
print( a:20,b:4);
System.out.println("
print2( a: 10);
System.out.println("-
print3( b: 5);
public static void print(int a,int b){...]
public static void print2(int a){...}
public static void print3(int b){ 1个用法
int a=100.
a+=b;
System.out.println(a);
byte a1=10;
byte a2=20;
a1+=a2;
System.out.println(a1);
  }
}

4.关系运算符,三元运算符

5逻辑运算符

特点:

&(与):一假为假

&&:一假为假,前面一个为假,后面一个人就不再执行

|(或):一真为真

||:一真为真,前面一个为真,后面一个人就不再执行

!(非):你真我假,你假我真

^(异或):相同是假,不同是真

三.程序流程控制

1.分支结构

一.if分支

1.结构
if(a>){
  int a = 123;
}else if{
int b = 123;
}else
int c =123;
}

2.switch分支

public static void test1(){ 0个用法System.out.println("输入性别");
Scanner sc =new Scanner(System.in);
String sex = sc.nextLine();
switch(sex){
case"男"
System.out.println("==").break;
case"女"
System.out.println("==").break;
default:
System.out.println('==");

注意:1.表达式的类型只能是byte,short,int,char,不支持double,float,long

2.输出的值不重复,只能是字面量,不能是字面量。

3.不要忘记写break

二.循环结构

1.for循环

public class D3 {
public static void main(String[] args){
public static void start(){ 0个用法for(int i=0;i<3;i++){System.out.println("hello");
System.out.println("for(int i=0;i<6;i++){
//i=1 3 5 7
System.out.println("hello");
结构

for(初始化语句;循环条件;迭代语句){

循环体语句

}

2.while循环

结构

while(循环条件){

循环体语句(被重复执行的代码)

迭代语句;

}

public static void test(){ 1个用法
int i=0;
while (i<6){
System.out.println("hello");
i++;
  }
    }
public static inttest2(){1 个用法
double money = 10000;
double rate=0.017;int year=0;
while(money<20000){
year++;
money=money*(1+rate);
}
return year;
 }

3.do while循环

结构

初始化语句

do{

循环体语句;

迭代语句;

}while(循环条件)

int i =0;
do {
System.out.println(“Hello World!");
i++;
}while(i< 3);
public static void test3(){ 1个用法
int i=0;
do{
System.out.println("hello");
i++;}while (i<3);
  }
三种循环的使用区别

1.for跟while的使用

知道具体的次数用for,反之用while

2.for和while是先判断后执行,do...while是先执行后判断

其他区别:for循环中,控制变量只在循环中使用,while循环中,控制循环的变量在循环后还可以继续使用

四.数组

1.数组是什么?

数组是一个容器,用来储存同一批类型的数据

2.格式

数据类型[]数组名 或者 数据类型 数组名[]

1.一维数组

1.数据类型

数据类型 [] 数组名=new 数据类型[长度]

int[] arr=new int[3]

public class D4 {
public static void main(String[] args){
inputScore();
}
public static void inputScore(){ 1个用法
//数据类型[]数组名 =new 数据类型[长度]
doublel [] scores=new double[8];
Scanner sc=new Scanner(System.in);
for(int i=0;i< scores.length; i++{
System.out.println("请输入第"+(i+1)+"个学生的成绩");
scores[i]= sc.nextDouble();
}
double allScore = 0.0;
for(int i=0;i< scores.length; i++){
double score =scoresli];
allScore += score;
}
System.out.println("平均分:"+allScore);
  }
}
public class D4 {
public static void main(String[] args){
//inputScore();
System.out.println("==========");
int[] scores={15,9000 ,10000,20000,9500,-5};
int max=getMax(scores);
System.out.println("最大颜值是"+max);
}
public static void inputScore()
public static int getMax(int[] arr){ 1个用法
int max = arr[0];
for(int i=1;i< arr.length; i++){if(arr[i]> max)
max = arr[i];
  }
}
return maX;
  }
}

2.面向对象

1.什么是对象?

用类创建出来记录数据的特殊结构

Public class 类名{

类名 对象名=new类名();

...

}

2.具体怎么操作

先创建一个类,再通过关键字new去得到一个新的对象

public class Star { 
String name;
int age;
String gender;
double weight;
double height;
}
public class Test {
public static void main(String[] args){
Star s1 = new Star();
s1.name="John";
s1.age=18;
s1.gender="男”
s1.height=180;
s1.weight=60;
System.out.println(s1.name);
System.out.println(s1.age);
System.out.println(s1.gender);
System.out.println(s1.height);
System.out.println(s1.weight);

Star s2 = new Star();
s2.name='Rose",
s2.age=18;
s2.gender="女";
s2.height=170;
s2.weight=50;
System.out.println(s2.name);
System.out.println(s2.age);
System.out.println(s2.gender);
System.out.println(s2.height);
System.out.println(s2.weight);
  }
}

3.类的基本语法

一.构造器

1.什么是构造器?

(1).无参数构造器:不能写返回值类型,名称必须是类名

(2).有参数构造器:有返回值类型

public class student { 
String name;
int age;
char sex;


public student(){ 
System.out.println("-----无参构造器-----");
public student(string n){ 
System.out.println("-----有参string构造器----")
public student(String n,int a, char s){ 1个用法
name=n;
age=a;
sex=S;


2.特点:

创建对象时会自动去调用构造器

3.注意事项:

类默认自带无参构造器

二.this关键字

1.什么是this关键字?

是一个变量,哪个对象调用方法,this就指向那个对象

2.具体的用途

用来解决对象的成员变量与方法内部变量名称一样时,导致访问冲突问题的

public class star { 
String name;
public void print(){ System.out.println(this);
System.out.println(this.name);
public void printHobby(String n){
System.out.println(name+"喜欢"+n);
}

三.封装

1.什么是封装?

类设计对象处理某一个事物的数据,把要处理的数据和以及处理这些数据的方法,设计到一个对象中去

三大特征:封装,继承,多态
2.设计规范是什么?

合理隐藏,合理暴露

公开成员用public(公开)进行修饰

隐藏成员,使用private(私有,隐藏)进行修饰

public class Student { 
//public int age;
String name;
//隐藏:private
private int age;
double math; 
double chinese; 
//暴露:使用public修饰(公开)的get和set方法
public void setAge(int age){
if(age < 0){
this.age = age;
  }
}
public int getAge(){ 
return age;
}
public void printAllscore(){ 
System.out.println(name+"的总成绩是"+(chinese+math));
}
public void printAverageScore(){ 
System.out.println(name +"的平均成绩是'+(chinese+math)/2);
}

四.实体类

1.什么是实体类?

成员变量必须私有,且提供get,set方法,必须有无参构造器

2.特点

可以用来创建对象,和保存某个事物的数据

public class student {
private String name;
private double chinense;
private double math;

public Student(String name,double chinense,double math){
this.name = name;
this.chinense = chinense;
this.math = math;
}
public string getName(){
return name;
public void setName(String name){
this.name = name;
public double getChinense(){ 
return chinense;
public void setChinense(double chinense){ this.chinense = chinense;
public double getMath(){ 
return math;
public void setMath(double math){ 
this.math = math;
  }
}

public class Test {
public static void main(String[] args){
Student s=new Student( name:" ".chinense:100,math: 24);
s.setName("boniu");
s.setChinense(100);
s.setMath(100);
System.out.println(s.getName());
System.out.println(s.getChinense());
System.out.println(s.getMath());
Student s2 = new Student( name:"lisa",chinense: 100, math:100);
System.out.println(s2.getName());
System.out.println(s2.getchinense());
System.out.println(s2.getMath());
  }
}

五.Static

1.Static关键字是什么?

叫静态,用来修饰成员变量,成员方法

2.static修饰的成员变量是什么?

静态变量:有static修饰,属于类,只有一份会被类的全部对象共享

3.无static修饰的变量叫什么?特点?

实例变量:无static修饰,属于每个对象

public class Student{
//静态变量
static String name;
//实例变量
int age;
}

``java
1.
public class test {
public static void main(String[] args){
student.name="圆滑”;
System.out.println(student.name)
student s1=new student(),
s1.name="马冬梅"
student s2=new student();
s2.name="秋葵"
System.out.println(s1.name);
System.out.println(s2.name);
}
}

```java
package com.shitilei;
public class student {
static String name;
int age;
}
4.注意:

1.静态方法可以直接访问静态成员,不可以直接访问实例成员

2.实例方法既可以直接访问静态成员,也可以直接访问实例成员

3.实例方法中可以出现this关键字,静态方法不可以

4.继承

1.什么是继承

用关键字extends让一个类和另一个类建立起父子关系

Public calss B extends A{

}

2.子类能继承是什么?

继承父类的非私有成员

public class Test {
public static void main(String[] args){
Teacher t= new Teacher();
t.setName("dlei");
t.setSkill("java");
t.setsex('男');
System.out.println(t.getSkill());
System.out.println(t.getName());
System.out.println(t.getsex());
}


public class Teacher extends Peopel{ 
private string skill; 
public string getskill(){
return skill;
}
public void setSkill(string skill){ 
this.skill = skill;
}


public class ConsultantPeopel{ 
extends private int number; 
public int getNumber(){
return number;
}

public void setNumber(int number){ 
this.number = number;
}
3.什么是权限修饰符?

就是用来限制类中的成员(变量,方法,构造器)能够被访问的范围

private:只能本类
缺省:本类,同一个包中的类
protected:本类,同一个包中的类,子孙类中
public:任意位置

private<缺省<protected<public
public class Fu {
//只能当前类中访问
private void privateMethod(){ System.out.println("privateMethod");
}
//缺省:只能当前类中,同一个包下的其他类
void method(){ 
System.out.println("method");
}
//只能当前类中,同一个包下的其它类中,子类中访问
protected void protectedMethod(){ System.out.println("protectedMethod");
}
//任何类都可以访问
public void publicMethod(){ System.out.println("publicMethod");
}

public static void main(String[] args){
Fu fu = new Fu();
fu.method();
fu.protectedMethod();
fu.privateMethod();
fu.publicMethod();
}

4.特点:

(1).单继承:一个子类只能继承一个父类

(2).多层继承:java不支持多继承,但支持多层继承

(3).祖宗类:Java中所有的类都是object类的子类

(4).就近原则:优先访问自己类中,没有的才去访问父类

5.子类构造器的特点:

子类的全部构造器,都会先调用父类的构造器,在执行自己

public class Test {
public static void main(string[] args){
//目标:子类构造器调用父类构造器的应用场景,
Teacher teacher = new Teacher( name: "dlei", skil: "java", sex:M);
System.out.println(teacher.getName());
System.out.println(teacher.getSkill());
System.out.println(teacher.getsex());
  }
}

public class Teacher extends People{ 
private string skill; 
public Teacher(String name,string skill,char sex){ 
this.skill=skill;
public string getskill(){ 
return skill:
public void setskill(string skill){ 
this.skill = skill;
 }
}
public class People { 
private char sex; 
public string getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex(){ 
return sex;
}
public void setSex(char sex){ 
this.sex=sex;
}

5.多态

1.什么是多态?

在继承/实现中的一种现象(特征)

对象多态,行为多态

public class Test {
public static void main(string[] args){
Animal a1 = new Animal();
a1.run();//方法:编译看左边,运行看右边
System.out.println(a1.name);//成员变量:编译看左边,运行也看右边
Animal a2 = new Tortoise();
a2.run();//方法:编译看左边,运行看右边
System.out.println(a2.name);//成员变量:编译看左边,运行也看左边
}
}
public class Animal { 
String name="动物";

public void run(){ 
System.out.println("动物会跑~~~");
  }
}
public class Tortoise extends Animal { 
String name="乌龟";
@0verride 
public void run(){
System.out.println("跑的贼快~~~");
  }
}
public class wolf extends Animal { 
String name="狼";
@0verride 
public void run(){
System.out.println(“狼跑的贼快~~~);

//前提:有继承/实现关系,存在父类引用子类对象,存在方法重写

2.多态的好处?

便于扩展和维护,使用父类继承时可以接受一切子类对象,包容性和扩展性更强

不能调用子类的独有方法

3.多态下的类型转换

自动类型转换:父类 变量名 =new 子类();

People p=new teacher();

强制性转换:子类 变量名=(子类) 父类变量;

Teacher t= (Teacher)p;

注意事项:1.存在继承和实现关系就可进行转换

4.面向对象高级

一.final

1.认识final

代表最终的意思,用来修饰类、方法、变量。

final关键字是最终的意思,可以修饰:类、方法、变量。
1.修饰类:该类被称为最终类,特点是不能被继承了
2.修饰方法:该方法被称为最终方法,特点是不能被重写了。
3.修饰变量:该变量有且仅能被赋值一次。

final修饰变量的注意
1.final修饰基本类型的变量,变量存储的数据不能被改变。
2.final修饰引用类型的变量,变量存储的地址不能被改变,但地址所指向对象的内容是可以被改变的。
public class D1 {
    //final修饰静态成员变量
    //final 修饰静态变量,这个变量季候被称为敞亮,可以记住一个固定值,并且在程序中不能修改,通常这个值作为系统的配置信,
    //常量的名称。建议全部大写,多个单词用下划线
    public final String schoolName= "ding";

//final 修饰实例变量(一般没有意义)
    private final  String name="猪八戒";


    public static void main(String[] args) {
//3.fianl修饰变量:变量有且仅能被赋值一次

        final double rate = 3.14;
        //rate=2.16
        buy(z:0.8);
    }

    public static  void buy(final double z){
        System.out.println(z);
}
//1.final修饰类,类不能被继承
final class A{}
//class B extends  A{}

//2.final 修饰类, 方法不能被重写
class C{
    public void show(){
        System.out.println("show方法被执行");
    }
}
class D extends  C{
    //@Override
    //public void show(){
       // System.out.println("show方法被执行");
    }
}
posted @ 2025-05-07 22:51  LaixF  阅读(15)  评论(0)    收藏  举报