简体   繁体   English

适用于Android Studio应用的Java中的OOP

[英]OOP in java for Android studio app

Im new to java, although have about a years experience with python and programming in general. 我是Java的新手,尽管一般来说他在python和编程方面有大约多年的经验。 I somewhat understand object oriented programming although since just recently teaching myself java, I dont understand how/why a piece of code works. 尽管自从最近自学Java以来​​,我还是对面向对象的编程有所了解,但是我不了解一段代码的工作方式/原因。 The code is for an android app created in android studio, where I am trying to learn how to create an app using the google maps API. 该代码适用于在android studio中创建的android应用,我试图在其中学习如何使用Google Maps API创建应用。 The java code is this: Java代码是这样的:

package com.example.harry.myapplication;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

As I understand it, this code creates the class, but I cant see where an object of the class is produced in order for the code to be run. 据我了解,该代码创建了该类,但是我看不到在哪里生成该类的对象才能运行该代码。 All of my programming experience has come from python/procedural programming. 我所有的编程经验都来自python /过程编程。

In addition to not fully understanding how this code works, if i was to call the method .getUiSettings(), what object would I call this on? 除了不完全了解此代码的工作方式之外,如果我要调用方法.getUiSettings(),我将在哪个对象上调用它?

What you have is your class definition -- ie a set of properties and methods that belong to your object, in this case MapsActivity . 您所拥有的是类定义-即属于对象的一组属性和方法,在本例中为MapsActivity When you go onto an Android device and open your app, the Android operating system reads your app's AndroidManifest.xml to determine which Activity is your app's main activity. 当您使用Android设备打开应用程序时,Android操作系统将读取应用程序的AndroidManifest.xml以确定哪个活动是您应用程序的主要活动。 (If you made this in Android Studio, I'm guessing that this activity was automatically designated as the main activity, and you shouldn't have to change anything.) The Android operating system creates an instance of that class, and then calls a specific sequence of methods to alert your object that certain things are happening. (如果您在Android Studio中进行了此操作,那么我猜想该活动将自动指定为主要活动,而您不必进行任何更改。)Android操作系统会创建该类的实例,然后调用方法的特定顺序,以警告您的对象某些事情正在发生。

getUiSettings() is a method of the GoogleMap object. getUiSettings()GoogleMap对象的方法。 In your activity's onCreate() method, you call getMapAsync() . 在活动的onCreate()方法中,调用getMapAsync() This launches a background thread to download the map information from Google without blocking up your main thread. 这将启动一个后台线程,以从Google下载地图信息,而不会阻塞您的主线程。 When that information has been fully received, onMapReady() is called with the prepared GoogleMap as the parameter. 完全接收到该信息后,将使用准备好的GoogleMap作为参数调用onMapReady() You then save this object for later use with 然后,您可以保存该对象以供以后使用

mMap = googleMap;

If you wanted to access the map's UI settings, you'd call the method on your map object 如果要访问地图的UI设置,可以在地图对象上调用方法

mMap.getUiSettings();

Android coding is very much based around asynchronicity and callbacks and can be a little confusing at first. Android编码很大程度上基于异步性和回调,一开始可能会有些混乱。 I would agree with @Vucko that perhaps you should find an online course or a more experienced programmer to learn from. 我同意@Vucko的观点,也许您应该找到在线课程或更有经验的程序员来学习。

The map object is created by the map fragment asynchronously and returned to the listener, your activity. 地图对象是由地图片段异步创建的,并返回给侦听器,即您的活动。 And getUiSettings would be called on the GoogleMap object. 并且getUiSettings将在GoogleMap对象上调用。 As you can see in the docs it's a method of the object. 如您在文档中所见,它是对象的方法。

As mentioned in comments, maybe SO isn't the place for this question. 如评论中所述,SO可能不是解决此问题的地方。 I found out recently there is a code review site in the stack exchange network. 我发现最近在堆栈交换网络中有一个代码检查站点 Maybe it's more appropriate. 也许更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM