Java基础学习之类与对象

面向对象简述:

java是一门纯面向对象的语言(Object Oriented Program,继承OOP),在java面向对象的世界里,一切皆为对象。面向对象是解决问题的一种思想,主要是要依靠对象之间的交互完成一件事情。用面向对象的思想来涉及程序,更符合人们对于事物的认知,对于大型程序的设计,扩展以及维护都非常友好。

三个基本特征:

(1)封装:保护内部的操作不被破坏,隐藏代码的实现细节;
(2)继承:在父类的基础之上继续在子类内部进行扩充;
(3)多态:通俗来说,就是多种形态,具体点就是去完成某个行为,当不同的对象去完成时会产生出不同。


类的概念:

类是对现实生活中一类具有共同属性和行为的事物的抽象。

类的特点:

类是对象的数据类型。

类是具有相同属性和行为的一组对象的集合

对象的概念:

既表示客观世界问题空间中的某个具体的事物,又表示软件系统解空间中的基本元素。

对象的属性

对象所具有的各种特征,每个对象的每个特征都拥有特定的值。

对象的行为

对象能够执行的操作。

类和对象的说明:

1.类是对象的模板。

2.对象是这个类的具体实现。

3.类是一种自定义的类型,可以用来定义变量。

4.实例化出的对象才能实际存储数据,占用空间。


题目一

 静态成员的访问   

先定义GeneralFunction类,GeneralFunction中包含静态的int成员变量x和y, 定义静态方法add(int x,  int y ),用来计算并返回静态成员变量x与y的和;然后再定义一个可执行类,采用类名直接调用和对象调用静态成员两种方法调用静态方法add(int x,  int y ), 并输出结果。

代码如下:

import java.util.Scanner;
public class GeneralFunction {

    public  static int add(int x,int y){
        return x+y;
    }

    public static void main(String[] args) {
        Scanner s1=new Scanner(System.in);
        System.out.println("输入数字x:");
        int x=s1.nextInt();
        Scanner s2=new Scanner(System.in);
        System.out.println("输入数字y:");
        int y=s2.nextInt();
        int z=GeneralFunction.add(x,y);
        System.out.print("采用类名直接调用方式:");
        System.out.println(x+"+"+y+"="+z);

        Scanner s3=new Scanner(System.in);
        System.out.println("输入数字a:");
        int a=s1.nextInt();
        Scanner s4=new Scanner(System.in);
        System.out.println("输入数字b:");
        int b=s2.nextInt();
        GeneralFunction sc=new GeneralFunction();
        int c=sc.add(a,b);
        System.out.print("采用对象调用的方式:");
        System.out.println(a+"+"+b+"="+c);
    }
}

结果如下:

输入数字x:
9
输入数字y:
10
采用类名直接调用方式:9+10=19
输入数字a:
19
输入数字b:
110
采用对象调用的方式:19+110=129

题目二

数组元素的赋值与输出  

创建一个整型数组intArrayA,它有30个元素,并将它的各个元素赋值如下公式,然后输出该数组。

intArrayA [0] =1, intArrayA [1] = 2

intArrayA [i] = intArrayA[i-1] + intArrayA [i-2]

输出内容如下:

1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269

代码如下:

public class intArray {
    public static void main(String[] args) {
        int []arr=new int[30];
        arr[0]=1;
        arr[1]=2;
        System.out.print(arr[0]+" ");
        System.out.print(arr[1]+" ");
        for(int i=2;i<30;i++){
            arr[i]=arr[i-1]+arr[i-2];
            System.out.print(arr[i]+" ");
        }
    }
}

结果如下:

1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 
Process finished with exit code 0

题目三

包的导入应用:从键盘读入数据   

编写一个程序,从键盘输入一个六数位整数x,判断并输出x中每一位数字的奇偶性。

代码如下:

import java.util.Scanner;
public class Math {
    public void math(int i){
        if(i%2==0){
            System.out.println("是偶数");
        }else{
            System.out.println("是奇数");
        }
    }
public static void main(String[] args) {
        Math num=new Math();
        System.out.println("输入一位六位数整数:");
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        int a=x%10;
        System.out.print("个位上的数是"+a+":");
        num.math(a);
        int b=(x%100-x%10)/10;
        System.out.print("十位上的数是"+b+":");
        num.math(b);
        int c=(x%1000-x%100)/100;
        System.out.print("百位上的数是"+c+":");
        num.math(c);
        int d=(x%10000-x%1000)/1000;
        System.out.print("千位上的数是"+d+":");
        num.math(d);
        int e=(x%100000-x%10000)/10000;
        System.out.print("万位上的数是"+e+":");
        num.math(e);
        int f=(x%1000000-x%10000)/100000;
        System.out.print("十万位上的数是"+f+":");
        num.math(f);

    }

}

结果如下:

输入一位六位数整数:
123456
个位上的数是6:是偶数
十位上的数是5:是奇数
百位上的数是4:是偶数
千位上的数是3:是偶数
万位上的数是2:是偶数
十万位上的数是1:是奇数

Process finished with exit code 0

题目四

最大值运算   

编写程序,要求实现以下功能:

1)通过编写max方法,该方法的功能是对给定的三个数(可从键盘输入),求出最大值。

2)使用main(String[] args)来调用max方法进行最大值运算,并输出结果。

代码如下:

import java.util.Scanner;

public class Max {
    public void MAX(){
        System.out.println("请输入第一个数:");
        Scanner sc1=new Scanner(System.in);
        int x=sc1.nextInt();

        System.out.println("请输入第二个数:");
        Scanner sc2=new Scanner(System.in);
        int y=sc2.nextInt();

        System.out.println("请输入第三个数:");
        Scanner sc3=new Scanner(System.in);
        int z=sc3.nextInt();

        int max=((z>((x>y)?x:y))?z:((x>y)?x:y));
        System.out.println("最大数为:"+max);
    }



    public static void main(String[] args) {
        Max a=new Max();
        a.MAX();
    }
}

结果如下:

请输入第一个数:
456
请输入第二个数:
678
请输入第三个数:
906
最大数为:906

Process finished with exit code 0

题目五

接口与类   

按以下要求编写程序                                         

1)编写Human接口,接口中声明sing() 方法。

2)定义American类和Englishman类实现Human接口。

3)输出American类和Englishman类中sing() 方法的内容。

4)创建测试类进行调试。

输出:

美国人唱的是美国的歌曲!

英国人唱的是英国的歌曲!

代码如下:

package test6;
/*编写Human接口,接口中声明sing() 方法*/
public interface Human {
    public abstract void sing();
}

package test6;
/*定义American类实现Human接口*/

public class American implements Human{
    public American() {
    }

    @Override
    public void sing() {
        System.out.println("美国人唱的是美国的歌曲!");
    }
}

package test6;
/*定义Englishman类实现Human接口*/

public class Englishman implements Human{
    public Englishman() {
    }

    @Override
    public void sing() {
        System.out.println("英国人唱的是英国的歌曲!");
    }
}

package test6;
/*测试类*/
public class HumanSingDome {
    public static void main(String[] args) {
        American a=new American();
        a.sing();

        Englishman e=new Englishman();
        e.sing();
    }
}

结果如下:

美国人唱的是美国的歌曲!
英国人唱的是英国的歌曲!

Process finished with exit code 0

题目六

计算并输出圆的周长      

按以下要求编写程序                                         

1)创建一个Circle类,添加radius(半径)成员变量。

2)定义计算并输出圆的周长的方法。

当添加radius(半径)成员变量的值为10.58时,输出结果为:
The girth for the circle is :66.476099416
 

代码如下:

import java.util.Scanner;

public class Circle {
    public double radius;

    public Circle() {
    }

    public Circle(int radius) {
        this.radius = radius;
    }

    public void Perimeter(){
        System.out.println("Please inter the radius of the circle:");
        Scanner sc=new Scanner(System.in);
        radius=sc.nextDouble();
        double pai=3.1415926;
        double c=2*pai*radius;
        System.out.println("The girth for the circle is :"+c);

    }

    public static void main(String[] args) {
        Circle c=new Circle();
        c.Perimeter();
    }
}

结果如下:

Please inter the radius of the circle:
10.58
The girth for the circle is :66.476099416

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

重心不倾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值