Home  >  Article  >  Java  >  How Java and Scala implement object-oriented programming

How Java and Scala implement object-oriented programming

WBOY
WBOYforward
2023-05-07 13:46:07719browse

Scala package:

Basic syntax:

package package name.class name

The three major functions of Scala package:

Distinguish between those with the same name Class

When there are many classes, you can manage the class well

Control the access scope

The naming convention of the package name:

is usually in lowercase letters Dot

com.Company name.Project name.Business module name

Benefits of writing packages:

You can import the packages you write into the project through maven

The unique way of writing packages in Scala does not correspond to the folder and can exist independently

The picture below is the package I built under the class, which can be used like a normal package

How Java and Scala implement object-oriented programming

Packages can be nested

内层调用外层 不需要导包  atguigu包在techer下
外层调用内层 对象 需要导包

The following code:

package chapter04
object Test_01_package {
  def main(args: Array[String]): Unit = {
  }
}
//Scala独有的
package techer {
  import chapter04.techer.atguigu.Inner
  object test01t {
    def main(args: Array[String]): Unit = {
      println("hello  我是大哥 pack")
      //外层调用内层 对象 需要导包
          //Inner.main是下边那个包中的
      Inner.main(args)
    }
  }
  package atguigu {
    object Inner {
      def main(args: Array[String]): Unit = {
        println("Inner")
        //内层调用外层 不需要导包  atguigu包在techer下
        test01t.main(args)
      }
    }
  }
}

Package object:

In Scala, packages are also oriented Object, as long as it is inside the package (as long as it is inside, inner nesting is also possible), you can directly call the properties and methods of the package object

If the package is nested, overrides will occur situation, the properties and methods of the closest package will be called (the package that is closest will be called later and will overwrite the previously called one)

Guide package description:

Guide packages do not support embedding (After importing a package, you can only use the classes and objects below it, and you can no longer use the packages below it. If you want to use the methods in the package below it, you need to use . But when we import the package and directly import the package below it, It can be called directly without using . .Predef._


Classes and objects:
Class: can be regarded as a template
Object: represents specific things
When we use objects, we will Create objects using classes as templates

Define classes:

There can only be one public in a file in Java

The default is public in Scala but it is in a file Multiple companion classes and companion objects can be created in

Encapsulation:

There are only two types of properties in Scala, variable (var) and immutable (val) (separated read and write permissions-- --Similar to encapsulation in object-oriented (the get set method is encapsulated to separate read and write permissions))

In Scala, the type of property is directly separated by the type of the property.

In Scala, the type of the property has been Encapsulated into var----private val----private final

And the get set method has also been encapsulated (encapsulated into annotations, when using the get set method you need to introduce the annotation @BeanProperty--- -However, the scope of this annotation has only one attribute, which means that one attribute needs to be set with an annotation)

Constructor:

Object-oriented languages ​​basically have constructors (construction methods). If There is no constructor in the class. The default constructor is the empty parameter constructor.

There is also a constructor in Scala. Unlike Java,

Scala’s constructor is to add a () to the class name. It is the main constructor (() can be omitted when the main constructor has no parameters, which means that it can be constructed without parameters by default, similar to Java)

If there is a main constructor, there must be a slave constructor

The format of the auxiliary constructor definition that can be defined inside the main constructor is fixed. The first line of code inside the slave constructor must directly or indirectly call the main constructor

class Person03(name:String){
  var name1:String=name
def this()
  {
    this("唐不唐")
    println("hello world")
  }
}

Master-slave constructor Loading mechanism:

There is no class loading mechanism in Scala, so when the code directly calls the auxiliary constructor, it does not know the contents of the class, so the above requirements (from the first line of code inside the constructor The main constructor must be called directly or indirectly), this is the reason, get the class information (load it once), and then return to the constructor to continue execution after the call is completed

Decided according to your parameters in the main method Which constructor do you call?

From the constructor there is a sequence, the later one can only call the previous one (the constructor called must be before this constructor)

From the constructor The name is unified as this (distinguished by the number of basic parameters)

The main constructor is a parameter that can be passed as a property (if the parameter type is omitted, the default is val)

Inheritance :

Inherit keyword: extends

Basic syntax:

class subclass name extends parent class name {class body}

Subclass inherits the attributes and methods of the parent class

scala is single inheritanceThe essence of inheritance: In fact, it is completely different from Java

When creating objects of subclasses Scala will first create the object of the parent class and then create the subclass object in the outer layer (same as Java)

The essence of Scala inheritance is to inherit a constructor of the parent class (inherit that constructor (the number of parameters is The difference) will be called)

Abstract attributes:

means not writing the equal sign. To write abstract methods, you need to change the class to an abstract class.

When using abstract attributes You need to inherit it and then rewrite the properties and methods (there is no need to rewrite properties in Java, because the bottom layer of Scala encapsulates properties and has its own methods)

在重写抽象类中的非抽象方法的时候需要在重写的方法前面加上    override   关键字

子类调用父类的方法的时候使用关键字super  

子类对抽象属性进行实现,父类抽象属性可以用 var 修饰;

子类对非抽象属性重写,父类非抽象属性只支持 val 类型,而不支持 var。
因为 var 修饰的为可变变量,子类继承之后就可以直接使用(可以直接进行修改),没有必要重写    

多态:

父类的引用指向子类的实例

Java中的多态测试

:在父子共有的属性和方法调用的是父类还是子类:

public class Polymorphic {
    public static void main(String[] args) {
        Person person=new Student();
        System.out.println(person.name);
        person.sayhi();
    }
    public static class Person{
     String   name="Person";
     public void sayhi()
     {
         System.out.println("person  sayhi");
     }
    }
public static class Student extends Person{
        String name="Student";
        public void sayhi()
        {
            System.out.println("student sayhi");
        }
}
 
}

属性是调用父类   方法是调用子类   

在进行多态的时候是先将整个内存先把父类写入里面,再把子类嵌套到外边

引用是使用栈 把地址值是先指向父类的   指向谁就调用谁的属性,但是调用方法是一层一层的调用,是不断的被重写的,所以方法是调用子类

How Java and Scala implement object-oriented programming

而Scala与Java不同 都是调用的子类的

Scala测试如下:
package chapter04
 
object Test07_Polymorphic {
  def main(args: Array[String]): Unit = {
       val  per:Person07=new Student07
       per.sayhi();
    println(per.name)
  }
}
class Person07()
{
  val name:String="dsdsd"
def sayhi():Unit={
  println("hi person")
}
}
class Student07 extends Person07{
  override val name: String = "Student"
override def sayhi(): Unit =
  {
    println("Student say hi")
  }
}

How Java and Scala implement object-oriented programming

匿名子类:

可以使用匿名子类直接调用抽象类

也可以直接new这个抽象子类

匿名子类是自动使用多态的

多态无法调用子类独有的属性和方法,外部无法使用匿名子类中特有的(它自己的)方法和属性

The above is the detailed content of How Java and Scala implement object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete